1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/kernel.h>
4 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/platform_device.h>
14 #include <linux/workqueue.h>
16 #include <adf_accel_devices.h>
17 #include <adf_common_drv.h>
19 #include <adf_dbgfs.h>
20 #include "adf_c62xvf_hw_data.h"
22 static const struct pci_device_id adf_pci_tbl
[] = {
23 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_INTEL_QAT_C62X_VF
), },
26 MODULE_DEVICE_TABLE(pci
, adf_pci_tbl
);
28 static int adf_probe(struct pci_dev
*dev
, const struct pci_device_id
*ent
);
29 static void adf_remove(struct pci_dev
*dev
);
31 static struct pci_driver adf_driver
= {
32 .id_table
= adf_pci_tbl
,
33 .name
= ADF_C62XVF_DEVICE_NAME
,
38 static void adf_cleanup_pci_dev(struct adf_accel_dev
*accel_dev
)
40 pci_release_regions(accel_dev
->accel_pci_dev
.pci_dev
);
41 pci_disable_device(accel_dev
->accel_pci_dev
.pci_dev
);
44 static void adf_cleanup_accel(struct adf_accel_dev
*accel_dev
)
46 struct adf_accel_pci
*accel_pci_dev
= &accel_dev
->accel_pci_dev
;
47 struct adf_accel_dev
*pf
;
50 for (i
= 0; i
< ADF_PCI_MAX_BARS
; i
++) {
51 struct adf_bar
*bar
= &accel_pci_dev
->pci_bars
[i
];
54 pci_iounmap(accel_pci_dev
->pci_dev
, bar
->virt_addr
);
57 if (accel_dev
->hw_device
) {
58 switch (accel_pci_dev
->pci_dev
->device
) {
59 case PCI_DEVICE_ID_INTEL_QAT_C62X_VF
:
60 adf_clean_hw_data_c62xiov(accel_dev
->hw_device
);
65 kfree(accel_dev
->hw_device
);
66 accel_dev
->hw_device
= NULL
;
68 adf_dbgfs_exit(accel_dev
);
69 adf_cfg_dev_remove(accel_dev
);
70 pf
= adf_devmgr_pci_to_accel_dev(accel_pci_dev
->pci_dev
->physfn
);
71 adf_devmgr_rm_dev(accel_dev
, pf
);
74 static int adf_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
76 struct adf_accel_dev
*accel_dev
;
77 struct adf_accel_dev
*pf
;
78 struct adf_accel_pci
*accel_pci_dev
;
79 struct adf_hw_device_data
*hw_data
;
80 unsigned int i
, bar_nr
;
81 unsigned long bar_mask
;
84 switch (ent
->device
) {
85 case PCI_DEVICE_ID_INTEL_QAT_C62X_VF
:
88 dev_err(&pdev
->dev
, "Invalid device 0x%x.\n", ent
->device
);
92 accel_dev
= kzalloc_node(sizeof(*accel_dev
), GFP_KERNEL
,
93 dev_to_node(&pdev
->dev
));
97 accel_dev
->is_vf
= true;
98 pf
= adf_devmgr_pci_to_accel_dev(pdev
->physfn
);
99 accel_pci_dev
= &accel_dev
->accel_pci_dev
;
100 accel_pci_dev
->pci_dev
= pdev
;
102 /* Add accel device to accel table */
103 if (adf_devmgr_add_dev(accel_dev
, pf
)) {
104 dev_err(&pdev
->dev
, "Failed to add new accelerator device.\n");
108 INIT_LIST_HEAD(&accel_dev
->crypto_list
);
110 accel_dev
->owner
= THIS_MODULE
;
111 /* Allocate and configure device configuration structure */
112 hw_data
= kzalloc_node(sizeof(*hw_data
), GFP_KERNEL
,
113 dev_to_node(&pdev
->dev
));
118 accel_dev
->hw_device
= hw_data
;
119 adf_init_hw_data_c62xiov(accel_dev
->hw_device
);
121 /* Get Accelerators and Accelerators Engines masks */
122 hw_data
->accel_mask
= hw_data
->get_accel_mask(hw_data
);
123 hw_data
->ae_mask
= hw_data
->get_ae_mask(hw_data
);
124 accel_pci_dev
->sku
= hw_data
->get_sku(hw_data
);
126 /* Create device configuration table */
127 ret
= adf_cfg_dev_add(accel_dev
);
131 /* enable PCI device */
132 if (pci_enable_device(pdev
)) {
137 /* set dma identifier */
138 ret
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(48));
140 dev_err(&pdev
->dev
, "No usable DMA configuration\n");
141 goto out_err_disable
;
144 if (pci_request_regions(pdev
, ADF_C62XVF_DEVICE_NAME
)) {
146 goto out_err_disable
;
149 /* Find and map all the device's BARS */
151 bar_mask
= pci_select_bars(pdev
, IORESOURCE_MEM
);
152 for_each_set_bit(bar_nr
, &bar_mask
, ADF_PCI_MAX_BARS
* 2) {
153 struct adf_bar
*bar
= &accel_pci_dev
->pci_bars
[i
++];
155 bar
->base_addr
= pci_resource_start(pdev
, bar_nr
);
158 bar
->size
= pci_resource_len(pdev
, bar_nr
);
159 bar
->virt_addr
= pci_iomap(accel_pci_dev
->pci_dev
, bar_nr
, 0);
160 if (!bar
->virt_addr
) {
161 dev_err(&pdev
->dev
, "Failed to map BAR %d\n", bar_nr
);
163 goto out_err_free_reg
;
166 pci_set_master(pdev
);
167 /* Completion for VF2PF request/response message exchange */
168 init_completion(&accel_dev
->vf
.msg_received
);
170 adf_dbgfs_init(accel_dev
);
172 ret
= adf_dev_up(accel_dev
, false);
174 goto out_err_dev_stop
;
179 adf_dev_down(accel_dev
);
181 pci_release_regions(accel_pci_dev
->pci_dev
);
183 pci_disable_device(accel_pci_dev
->pci_dev
);
185 adf_cleanup_accel(accel_dev
);
190 static void adf_remove(struct pci_dev
*pdev
)
192 struct adf_accel_dev
*accel_dev
= adf_devmgr_pci_to_accel_dev(pdev
);
195 pr_err("QAT: Driver removal failed\n");
198 adf_flush_vf_wq(accel_dev
);
199 adf_dev_down(accel_dev
);
200 adf_cleanup_accel(accel_dev
);
201 adf_cleanup_pci_dev(accel_dev
);
205 static int __init
adfdrv_init(void)
207 request_module("intel_qat");
209 if (pci_register_driver(&adf_driver
)) {
210 pr_err("QAT: Driver initialization failed\n");
216 static void __exit
adfdrv_release(void)
218 pci_unregister_driver(&adf_driver
);
219 adf_clean_vf_map(true);
222 module_init(adfdrv_init
);
223 module_exit(adfdrv_release
);
225 MODULE_LICENSE("Dual BSD/GPL");
226 MODULE_AUTHOR("Intel");
227 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
228 MODULE_VERSION(ADF_DRV_VERSION
);
229 MODULE_IMPORT_NS("CRYPTO_QAT");