1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2020 Intel Corporation */
3 #include <linux/device.h>
4 #include <linux/module.h>
7 #include <adf_accel_devices.h>
9 #include <adf_common_drv.h>
11 #include "adf_4xxx_hw_data.h"
12 #include "qat_crypto.h"
13 #include "adf_transport_access_macros.h"
15 static const struct pci_device_id adf_pci_tbl
[] = {
16 { PCI_VDEVICE(INTEL
, ADF_4XXX_PCI_DEVICE_ID
), },
19 MODULE_DEVICE_TABLE(pci
, adf_pci_tbl
);
21 static void adf_cleanup_accel(struct adf_accel_dev
*accel_dev
)
23 if (accel_dev
->hw_device
) {
24 adf_clean_hw_data_4xxx(accel_dev
->hw_device
);
25 accel_dev
->hw_device
= NULL
;
27 adf_cfg_dev_remove(accel_dev
);
28 debugfs_remove(accel_dev
->debugfs_dir
);
29 adf_devmgr_rm_dev(accel_dev
, NULL
);
32 static int adf_crypto_dev_config(struct adf_accel_dev
*accel_dev
)
34 char key
[ADF_CFG_MAX_KEY_LEN_IN_BYTES
];
35 int banks
= GET_MAX_BANKS(accel_dev
);
36 int cpus
= num_online_cpus();
37 unsigned long bank
, val
;
42 if (adf_hw_dev_has_crypto(accel_dev
))
43 instances
= min(cpus
, banks
/ 2);
47 ret
= adf_cfg_section_add(accel_dev
, ADF_KERNEL_SEC
);
51 ret
= adf_cfg_section_add(accel_dev
, "Accelerator0");
55 for (i
= 0; i
< instances
; i
++) {
58 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_ASYM_BANK_NUM
, i
);
59 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
65 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_SYM_BANK_NUM
, i
);
66 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
71 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_ETRMGR_CORE_AFFINITY
,
73 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
78 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_ASYM_SIZE
, i
);
80 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
86 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_SYM_SIZE
, i
);
87 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
93 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_ASYM_TX
, i
);
94 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
100 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_SYM_TX
, i
);
101 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
107 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_ASYM_RX
, i
);
108 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
114 snprintf(key
, sizeof(key
), ADF_CY
"%d" ADF_RING_SYM_RX
, i
);
115 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
,
120 val
= ADF_COALESCING_DEF_TIME
;
121 snprintf(key
, sizeof(key
), ADF_ETRMGR_COALESCE_TIMER_FORMAT
, i
);
122 ret
= adf_cfg_add_key_value_param(accel_dev
, "Accelerator0",
129 ret
= adf_cfg_add_key_value_param(accel_dev
, ADF_KERNEL_SEC
, ADF_NUM_CY
,
134 set_bit(ADF_STATUS_CONFIGURED
, &accel_dev
->status
);
137 dev_err(&GET_DEV(accel_dev
), "Failed to start QAT accel dev\n");
141 static int adf_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
143 struct adf_accel_dev
*accel_dev
;
144 struct adf_accel_pci
*accel_pci_dev
;
145 struct adf_hw_device_data
*hw_data
;
146 char name
[ADF_DEVICE_NAME_LENGTH
];
147 unsigned int i
, bar_nr
;
148 unsigned long bar_mask
;
152 if (num_possible_nodes() > 1 && dev_to_node(&pdev
->dev
) < 0) {
154 * If the accelerator is connected to a node with no memory
155 * there is no point in using the accelerator since the remote
156 * memory transaction will be very slow.
158 dev_err(&pdev
->dev
, "Invalid NUMA configuration.\n");
162 accel_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*accel_dev
), GFP_KERNEL
);
166 INIT_LIST_HEAD(&accel_dev
->crypto_list
);
167 accel_pci_dev
= &accel_dev
->accel_pci_dev
;
168 accel_pci_dev
->pci_dev
= pdev
;
171 * Add accel device to accel table
172 * This should be called before adf_cleanup_accel is called
174 if (adf_devmgr_add_dev(accel_dev
, NULL
)) {
175 dev_err(&pdev
->dev
, "Failed to add new accelerator device.\n");
179 accel_dev
->owner
= THIS_MODULE
;
180 /* Allocate and initialise device hardware meta-data structure */
181 hw_data
= devm_kzalloc(&pdev
->dev
, sizeof(*hw_data
), GFP_KERNEL
);
187 accel_dev
->hw_device
= hw_data
;
188 adf_init_hw_data_4xxx(accel_dev
->hw_device
);
190 pci_read_config_byte(pdev
, PCI_REVISION_ID
, &accel_pci_dev
->revid
);
191 pci_read_config_dword(pdev
, ADF_4XXX_FUSECTL4_OFFSET
, &hw_data
->fuses
);
193 /* Get Accelerators and Accelerators Engines masks */
194 hw_data
->accel_mask
= hw_data
->get_accel_mask(hw_data
);
195 hw_data
->ae_mask
= hw_data
->get_ae_mask(hw_data
);
196 accel_pci_dev
->sku
= hw_data
->get_sku(hw_data
);
197 /* If the device has no acceleration engines then ignore it */
198 if (!hw_data
->accel_mask
|| !hw_data
->ae_mask
||
199 (~hw_data
->ae_mask
& 0x01)) {
200 dev_err(&pdev
->dev
, "No acceleration units found.\n");
205 /* Create dev top level debugfs entry */
206 snprintf(name
, sizeof(name
), "%s%s_%s", ADF_DEVICE_NAME_PREFIX
,
207 hw_data
->dev_class
->name
, pci_name(pdev
));
209 accel_dev
->debugfs_dir
= debugfs_create_dir(name
, NULL
);
211 /* Create device configuration table */
212 ret
= adf_cfg_dev_add(accel_dev
);
216 /* Enable PCI device */
217 ret
= pcim_enable_device(pdev
);
219 dev_err(&pdev
->dev
, "Can't enable PCI device.\n");
223 /* Set DMA identifier */
224 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
225 if ((pci_set_dma_mask(pdev
, DMA_BIT_MASK(32)))) {
226 dev_err(&pdev
->dev
, "No usable DMA configuration.\n");
230 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(32));
233 pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
236 /* Get accelerator capabilities mask */
237 hw_data
->accel_capabilities_mask
= hw_data
->get_accel_cap(accel_dev
);
239 /* Find and map all the device's BARS */
240 bar_mask
= pci_select_bars(pdev
, IORESOURCE_MEM
) & ADF_4XXX_BAR_MASK
;
242 ret
= pcim_iomap_regions_request_all(pdev
, bar_mask
, pci_name(pdev
));
244 dev_err(&pdev
->dev
, "Failed to map pci regions.\n");
249 for_each_set_bit(bar_nr
, &bar_mask
, PCI_STD_NUM_BARS
) {
250 bar
= &accel_pci_dev
->pci_bars
[i
++];
251 bar
->virt_addr
= pcim_iomap_table(pdev
)[bar_nr
];
254 pci_set_master(pdev
);
256 if (adf_enable_aer(accel_dev
)) {
257 dev_err(&pdev
->dev
, "Failed to enable aer.\n");
262 if (pci_save_state(pdev
)) {
263 dev_err(&pdev
->dev
, "Failed to save pci state.\n");
265 goto out_err_disable_aer
;
268 ret
= adf_crypto_dev_config(accel_dev
);
270 goto out_err_disable_aer
;
272 ret
= adf_dev_init(accel_dev
);
274 goto out_err_dev_shutdown
;
276 ret
= adf_dev_start(accel_dev
);
278 goto out_err_dev_stop
;
283 adf_dev_stop(accel_dev
);
284 out_err_dev_shutdown
:
285 adf_dev_shutdown(accel_dev
);
287 adf_disable_aer(accel_dev
);
289 adf_cleanup_accel(accel_dev
);
293 static void adf_remove(struct pci_dev
*pdev
)
295 struct adf_accel_dev
*accel_dev
= adf_devmgr_pci_to_accel_dev(pdev
);
298 pr_err("QAT: Driver removal failed\n");
301 adf_dev_stop(accel_dev
);
302 adf_dev_shutdown(accel_dev
);
303 adf_disable_aer(accel_dev
);
304 adf_cleanup_accel(accel_dev
);
307 static struct pci_driver adf_driver
= {
308 .id_table
= adf_pci_tbl
,
309 .name
= ADF_4XXX_DEVICE_NAME
,
311 .remove
= adf_remove
,
312 .sriov_configure
= adf_sriov_configure
,
315 module_pci_driver(adf_driver
);
317 MODULE_LICENSE("Dual BSD/GPL");
318 MODULE_AUTHOR("Intel");
319 MODULE_FIRMWARE(ADF_4XXX_FW
);
320 MODULE_FIRMWARE(ADF_4XXX_MMP
);
321 MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
322 MODULE_VERSION(ADF_DRV_VERSION
);
323 MODULE_SOFTDEP("pre: crypto-intel_qat");