1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Intel Corporation */
3 #include <linux/device.h>
4 #include <linux/module.h>
7 #include <adf_accel_devices.h>
8 #include <adf_gen4_hw_data.h>
9 #include <adf_gen4_config.h>
11 #include <adf_common_drv.h>
12 #include <adf_dbgfs.h>
14 #include "adf_420xx_hw_data.h"
16 static const struct pci_device_id adf_pci_tbl
[] = {
17 { PCI_VDEVICE(INTEL
, ADF_420XX_PCI_DEVICE_ID
), },
20 MODULE_DEVICE_TABLE(pci
, adf_pci_tbl
);
22 static void adf_cleanup_accel(struct adf_accel_dev
*accel_dev
)
24 if (accel_dev
->hw_device
) {
25 adf_clean_hw_data_420xx(accel_dev
->hw_device
);
26 accel_dev
->hw_device
= NULL
;
28 adf_dbgfs_exit(accel_dev
);
29 adf_cfg_dev_remove(accel_dev
);
30 adf_devmgr_rm_dev(accel_dev
, NULL
);
33 static int adf_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
35 struct adf_accel_dev
*accel_dev
;
36 struct adf_accel_pci
*accel_pci_dev
;
37 struct adf_hw_device_data
*hw_data
;
38 unsigned int i
, bar_nr
;
39 unsigned long bar_mask
;
43 if (num_possible_nodes() > 1 && dev_to_node(&pdev
->dev
) < 0) {
45 * If the accelerator is connected to a node with no memory
46 * there is no point in using the accelerator since the remote
47 * memory transaction will be very slow.
49 dev_err(&pdev
->dev
, "Invalid NUMA configuration.\n");
53 accel_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*accel_dev
), GFP_KERNEL
);
57 INIT_LIST_HEAD(&accel_dev
->crypto_list
);
58 accel_pci_dev
= &accel_dev
->accel_pci_dev
;
59 accel_pci_dev
->pci_dev
= pdev
;
62 * Add accel device to accel table
63 * This should be called before adf_cleanup_accel is called
65 if (adf_devmgr_add_dev(accel_dev
, NULL
)) {
66 dev_err(&pdev
->dev
, "Failed to add new accelerator device.\n");
70 accel_dev
->owner
= THIS_MODULE
;
71 /* Allocate and initialise device hardware meta-data structure */
72 hw_data
= devm_kzalloc(&pdev
->dev
, sizeof(*hw_data
), GFP_KERNEL
);
78 accel_dev
->hw_device
= hw_data
;
79 adf_init_hw_data_420xx(accel_dev
->hw_device
, ent
->device
);
81 pci_read_config_byte(pdev
, PCI_REVISION_ID
, &accel_pci_dev
->revid
);
82 pci_read_config_dword(pdev
, ADF_GEN4_FUSECTL4_OFFSET
, &hw_data
->fuses
);
84 /* Get Accelerators and Accelerators Engines masks */
85 hw_data
->accel_mask
= hw_data
->get_accel_mask(hw_data
);
86 hw_data
->ae_mask
= hw_data
->get_ae_mask(hw_data
);
87 accel_pci_dev
->sku
= hw_data
->get_sku(hw_data
);
88 /* If the device has no acceleration engines then ignore it */
89 if (!hw_data
->accel_mask
|| !hw_data
->ae_mask
||
90 (~hw_data
->ae_mask
& 0x01)) {
91 dev_err(&pdev
->dev
, "No acceleration units found.\n");
96 /* Create device configuration table */
97 ret
= adf_cfg_dev_add(accel_dev
);
101 /* Enable PCI device */
102 ret
= pcim_enable_device(pdev
);
104 dev_err(&pdev
->dev
, "Can't enable PCI device.\n");
108 /* Set DMA identifier */
109 ret
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
111 dev_err(&pdev
->dev
, "No usable DMA configuration.\n");
115 ret
= adf_gen4_cfg_dev_init(accel_dev
);
117 dev_err(&pdev
->dev
, "Failed to initialize configuration.\n");
121 /* Get accelerator capabilities mask */
122 hw_data
->accel_capabilities_mask
= hw_data
->get_accel_cap(accel_dev
);
123 if (!hw_data
->accel_capabilities_mask
) {
124 dev_err(&pdev
->dev
, "Failed to get capabilities mask.\n");
129 /* Find and map all the device's BARS */
130 bar_mask
= pci_select_bars(pdev
, IORESOURCE_MEM
) & ADF_GEN4_BAR_MASK
;
132 ret
= pcim_request_all_regions(pdev
, pci_name(pdev
));
134 dev_err(&pdev
->dev
, "Failed to request PCI regions.\n");
139 for_each_set_bit(bar_nr
, &bar_mask
, PCI_STD_NUM_BARS
) {
140 bar
= &accel_pci_dev
->pci_bars
[i
++];
141 bar
->virt_addr
= pcim_iomap(pdev
, bar_nr
, 0);
142 if (!bar
->virt_addr
) {
143 dev_err(&pdev
->dev
, "Failed to ioremap PCI region.\n");
149 pci_set_master(pdev
);
151 if (pci_save_state(pdev
)) {
152 dev_err(&pdev
->dev
, "Failed to save pci state.\n");
157 accel_dev
->ras_errors
.enabled
= true;
158 adf_dbgfs_init(accel_dev
);
160 ret
= adf_dev_up(accel_dev
, true);
162 goto out_err_dev_stop
;
164 ret
= adf_sysfs_init(accel_dev
);
166 goto out_err_dev_stop
;
171 adf_dev_down(accel_dev
);
173 adf_cleanup_accel(accel_dev
);
177 static void adf_remove(struct pci_dev
*pdev
)
179 struct adf_accel_dev
*accel_dev
= adf_devmgr_pci_to_accel_dev(pdev
);
182 pr_err("QAT: Driver removal failed\n");
185 adf_dev_down(accel_dev
);
186 adf_cleanup_accel(accel_dev
);
189 static struct pci_driver adf_driver
= {
190 .id_table
= adf_pci_tbl
,
191 .name
= ADF_420XX_DEVICE_NAME
,
193 .remove
= adf_remove
,
194 .sriov_configure
= adf_sriov_configure
,
195 .err_handler
= &adf_err_handler
,
198 module_pci_driver(adf_driver
);
200 MODULE_LICENSE("GPL");
201 MODULE_AUTHOR("Intel");
202 MODULE_FIRMWARE(ADF_420XX_FW
);
203 MODULE_FIRMWARE(ADF_420XX_MMP
);
204 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
205 MODULE_VERSION(ADF_DRV_VERSION
);
206 MODULE_SOFTDEP("pre: crypto-intel_qat");
207 MODULE_IMPORT_NS("CRYPTO_QAT");