xref: /linux/drivers/crypto/intel/qat/qat_420xx/adf_drv.c (revision 85ffc6e4ed3712f8b3fedb3fbe42afae644a699c)
1fcf60f4bSJie Wang // SPDX-License-Identifier: GPL-2.0-only
2fcf60f4bSJie Wang /* Copyright(c) 2023 Intel Corporation */
3fcf60f4bSJie Wang #include <linux/device.h>
4fcf60f4bSJie Wang #include <linux/module.h>
5fcf60f4bSJie Wang #include <linux/pci.h>
6fcf60f4bSJie Wang 
7fcf60f4bSJie Wang #include <adf_accel_devices.h>
8fcf60f4bSJie Wang #include <adf_gen4_hw_data.h>
9fcf60f4bSJie Wang #include <adf_gen4_config.h>
10fcf60f4bSJie Wang #include <adf_cfg.h>
11fcf60f4bSJie Wang #include <adf_common_drv.h>
12fcf60f4bSJie Wang #include <adf_dbgfs.h>
13fcf60f4bSJie Wang 
14fcf60f4bSJie Wang #include "adf_420xx_hw_data.h"
15fcf60f4bSJie Wang 
16fcf60f4bSJie Wang static const struct pci_device_id adf_pci_tbl[] = {
17fcf60f4bSJie Wang 	{ PCI_VDEVICE(INTEL, ADF_420XX_PCI_DEVICE_ID), },
18fcf60f4bSJie Wang 	{ }
19fcf60f4bSJie Wang };
20fcf60f4bSJie Wang MODULE_DEVICE_TABLE(pci, adf_pci_tbl);
21fcf60f4bSJie Wang 
adf_cleanup_accel(struct adf_accel_dev * accel_dev)22fcf60f4bSJie Wang static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)
23fcf60f4bSJie Wang {
24fcf60f4bSJie Wang 	if (accel_dev->hw_device) {
25fcf60f4bSJie Wang 		adf_clean_hw_data_420xx(accel_dev->hw_device);
26fcf60f4bSJie Wang 		accel_dev->hw_device = NULL;
27fcf60f4bSJie Wang 	}
28fcf60f4bSJie Wang 	adf_dbgfs_exit(accel_dev);
29fcf60f4bSJie Wang 	adf_cfg_dev_remove(accel_dev);
30fcf60f4bSJie Wang 	adf_devmgr_rm_dev(accel_dev, NULL);
31fcf60f4bSJie Wang }
32fcf60f4bSJie Wang 
adf_probe(struct pci_dev * pdev,const struct pci_device_id * ent)33fcf60f4bSJie Wang static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
34fcf60f4bSJie Wang {
35fcf60f4bSJie Wang 	struct adf_accel_dev *accel_dev;
36fcf60f4bSJie Wang 	struct adf_accel_pci *accel_pci_dev;
37fcf60f4bSJie Wang 	struct adf_hw_device_data *hw_data;
38fcf60f4bSJie Wang 	unsigned int i, bar_nr;
39fcf60f4bSJie Wang 	unsigned long bar_mask;
40fcf60f4bSJie Wang 	struct adf_bar *bar;
41fcf60f4bSJie Wang 	int ret;
42fcf60f4bSJie Wang 
43fcf60f4bSJie Wang 	if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {
44fcf60f4bSJie Wang 		/*
45fcf60f4bSJie Wang 		 * If the accelerator is connected to a node with no memory
46fcf60f4bSJie Wang 		 * there is no point in using the accelerator since the remote
47fcf60f4bSJie Wang 		 * memory transaction will be very slow.
48fcf60f4bSJie Wang 		 */
49fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Invalid NUMA configuration.\n");
50fcf60f4bSJie Wang 		return -EINVAL;
51fcf60f4bSJie Wang 	}
52fcf60f4bSJie Wang 
53fcf60f4bSJie Wang 	accel_dev = devm_kzalloc(&pdev->dev, sizeof(*accel_dev), GFP_KERNEL);
54fcf60f4bSJie Wang 	if (!accel_dev)
55fcf60f4bSJie Wang 		return -ENOMEM;
56fcf60f4bSJie Wang 
57fcf60f4bSJie Wang 	INIT_LIST_HEAD(&accel_dev->crypto_list);
58fcf60f4bSJie Wang 	accel_pci_dev = &accel_dev->accel_pci_dev;
59fcf60f4bSJie Wang 	accel_pci_dev->pci_dev = pdev;
60fcf60f4bSJie Wang 
61fcf60f4bSJie Wang 	/*
62fcf60f4bSJie Wang 	 * Add accel device to accel table
63fcf60f4bSJie Wang 	 * This should be called before adf_cleanup_accel is called
64fcf60f4bSJie Wang 	 */
65fcf60f4bSJie Wang 	if (adf_devmgr_add_dev(accel_dev, NULL)) {
66fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Failed to add new accelerator device.\n");
67fcf60f4bSJie Wang 		return -EFAULT;
68fcf60f4bSJie Wang 	}
69fcf60f4bSJie Wang 
70fcf60f4bSJie Wang 	accel_dev->owner = THIS_MODULE;
71fcf60f4bSJie Wang 	/* Allocate and initialise device hardware meta-data structure */
72fcf60f4bSJie Wang 	hw_data = devm_kzalloc(&pdev->dev, sizeof(*hw_data), GFP_KERNEL);
73fcf60f4bSJie Wang 	if (!hw_data) {
74fcf60f4bSJie Wang 		ret = -ENOMEM;
75fcf60f4bSJie Wang 		goto out_err;
76fcf60f4bSJie Wang 	}
77fcf60f4bSJie Wang 
78fcf60f4bSJie Wang 	accel_dev->hw_device = hw_data;
79fcf60f4bSJie Wang 	adf_init_hw_data_420xx(accel_dev->hw_device, ent->device);
80fcf60f4bSJie Wang 
81fcf60f4bSJie Wang 	pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid);
82fcf60f4bSJie Wang 	pci_read_config_dword(pdev, ADF_GEN4_FUSECTL4_OFFSET, &hw_data->fuses);
83fcf60f4bSJie Wang 
84fcf60f4bSJie Wang 	/* Get Accelerators and Accelerators Engines masks */
85fcf60f4bSJie Wang 	hw_data->accel_mask = hw_data->get_accel_mask(hw_data);
86fcf60f4bSJie Wang 	hw_data->ae_mask = hw_data->get_ae_mask(hw_data);
87fcf60f4bSJie Wang 	accel_pci_dev->sku = hw_data->get_sku(hw_data);
88fcf60f4bSJie Wang 	/* If the device has no acceleration engines then ignore it */
89fcf60f4bSJie Wang 	if (!hw_data->accel_mask || !hw_data->ae_mask ||
90fcf60f4bSJie Wang 	    (~hw_data->ae_mask & 0x01)) {
91fcf60f4bSJie Wang 		dev_err(&pdev->dev, "No acceleration units found.\n");
92fcf60f4bSJie Wang 		ret = -EFAULT;
93fcf60f4bSJie Wang 		goto out_err;
94fcf60f4bSJie Wang 	}
95fcf60f4bSJie Wang 
96fcf60f4bSJie Wang 	/* Create device configuration table */
97fcf60f4bSJie Wang 	ret = adf_cfg_dev_add(accel_dev);
98fcf60f4bSJie Wang 	if (ret)
99fcf60f4bSJie Wang 		goto out_err;
100fcf60f4bSJie Wang 
101fcf60f4bSJie Wang 	/* Enable PCI device */
102fcf60f4bSJie Wang 	ret = pcim_enable_device(pdev);
103fcf60f4bSJie Wang 	if (ret) {
104fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Can't enable PCI device.\n");
105fcf60f4bSJie Wang 		goto out_err;
106fcf60f4bSJie Wang 	}
107fcf60f4bSJie Wang 
108fcf60f4bSJie Wang 	/* Set DMA identifier */
109fcf60f4bSJie Wang 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
110fcf60f4bSJie Wang 	if (ret) {
111fcf60f4bSJie Wang 		dev_err(&pdev->dev, "No usable DMA configuration.\n");
112fcf60f4bSJie Wang 		goto out_err;
113fcf60f4bSJie Wang 	}
114fcf60f4bSJie Wang 
115fcf60f4bSJie Wang 	ret = adf_gen4_cfg_dev_init(accel_dev);
116fcf60f4bSJie Wang 	if (ret) {
117fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Failed to initialize configuration.\n");
118fcf60f4bSJie Wang 		goto out_err;
119fcf60f4bSJie Wang 	}
120fcf60f4bSJie Wang 
121fcf60f4bSJie Wang 	/* Get accelerator capabilities mask */
122fcf60f4bSJie Wang 	hw_data->accel_capabilities_mask = hw_data->get_accel_cap(accel_dev);
123fcf60f4bSJie Wang 	if (!hw_data->accel_capabilities_mask) {
124fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Failed to get capabilities mask.\n");
125fcf60f4bSJie Wang 		ret = -EINVAL;
126fcf60f4bSJie Wang 		goto out_err;
127fcf60f4bSJie Wang 	}
128fcf60f4bSJie Wang 
129fcf60f4bSJie Wang 	/* Find and map all the device's BARS */
130fcf60f4bSJie Wang 	bar_mask = pci_select_bars(pdev, IORESOURCE_MEM) & ADF_GEN4_BAR_MASK;
131fcf60f4bSJie Wang 
132fcf60f4bSJie Wang 	ret = pcim_iomap_regions_request_all(pdev, bar_mask, pci_name(pdev));
133fcf60f4bSJie Wang 	if (ret) {
134fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Failed to map pci regions.\n");
135fcf60f4bSJie Wang 		goto out_err;
136fcf60f4bSJie Wang 	}
137fcf60f4bSJie Wang 
138fcf60f4bSJie Wang 	i = 0;
139fcf60f4bSJie Wang 	for_each_set_bit(bar_nr, &bar_mask, PCI_STD_NUM_BARS) {
140fcf60f4bSJie Wang 		bar = &accel_pci_dev->pci_bars[i++];
141fcf60f4bSJie Wang 		bar->virt_addr = pcim_iomap_table(pdev)[bar_nr];
142fcf60f4bSJie Wang 	}
143fcf60f4bSJie Wang 
144fcf60f4bSJie Wang 	pci_set_master(pdev);
145fcf60f4bSJie Wang 
146fcf60f4bSJie Wang 	if (pci_save_state(pdev)) {
147fcf60f4bSJie Wang 		dev_err(&pdev->dev, "Failed to save pci state.\n");
148fcf60f4bSJie Wang 		ret = -ENOMEM;
149fcf60f4bSJie Wang 		goto out_err;
150fcf60f4bSJie Wang 	}
151fcf60f4bSJie Wang 
152fcf60f4bSJie Wang 	accel_dev->ras_errors.enabled = true;
153fcf60f4bSJie Wang 	adf_dbgfs_init(accel_dev);
154fcf60f4bSJie Wang 
155fcf60f4bSJie Wang 	ret = adf_dev_up(accel_dev, true);
156fcf60f4bSJie Wang 	if (ret)
157fcf60f4bSJie Wang 		goto out_err_dev_stop;
158fcf60f4bSJie Wang 
159fcf60f4bSJie Wang 	ret = adf_sysfs_init(accel_dev);
160fcf60f4bSJie Wang 	if (ret)
161fcf60f4bSJie Wang 		goto out_err_dev_stop;
162fcf60f4bSJie Wang 
163fcf60f4bSJie Wang 	return ret;
164fcf60f4bSJie Wang 
165fcf60f4bSJie Wang out_err_dev_stop:
166*4e190a57SAdam Guerin 	adf_dev_down(accel_dev);
167fcf60f4bSJie Wang out_err:
168fcf60f4bSJie Wang 	adf_cleanup_accel(accel_dev);
169fcf60f4bSJie Wang 	return ret;
170fcf60f4bSJie Wang }
171fcf60f4bSJie Wang 
adf_remove(struct pci_dev * pdev)172fcf60f4bSJie Wang static void adf_remove(struct pci_dev *pdev)
173fcf60f4bSJie Wang {
174fcf60f4bSJie Wang 	struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
175fcf60f4bSJie Wang 
176fcf60f4bSJie Wang 	if (!accel_dev) {
177fcf60f4bSJie Wang 		pr_err("QAT: Driver removal failed\n");
178fcf60f4bSJie Wang 		return;
179fcf60f4bSJie Wang 	}
180*4e190a57SAdam Guerin 	adf_dev_down(accel_dev);
181fcf60f4bSJie Wang 	adf_cleanup_accel(accel_dev);
182fcf60f4bSJie Wang }
183fcf60f4bSJie Wang 
184fcf60f4bSJie Wang static struct pci_driver adf_driver = {
185fcf60f4bSJie Wang 	.id_table = adf_pci_tbl,
186fcf60f4bSJie Wang 	.name = ADF_420XX_DEVICE_NAME,
187fcf60f4bSJie Wang 	.probe = adf_probe,
188fcf60f4bSJie Wang 	.remove = adf_remove,
189fcf60f4bSJie Wang 	.sriov_configure = adf_sriov_configure,
190fcf60f4bSJie Wang 	.err_handler = &adf_err_handler,
191fcf60f4bSJie Wang };
192fcf60f4bSJie Wang 
193fcf60f4bSJie Wang module_pci_driver(adf_driver);
194fcf60f4bSJie Wang 
195fcf60f4bSJie Wang MODULE_LICENSE("GPL");
196fcf60f4bSJie Wang MODULE_AUTHOR("Intel");
197fcf60f4bSJie Wang MODULE_FIRMWARE(ADF_420XX_FW);
198fcf60f4bSJie Wang MODULE_FIRMWARE(ADF_420XX_MMP);
199fcf60f4bSJie Wang MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");
200fcf60f4bSJie Wang MODULE_VERSION(ADF_DRV_VERSION);
201fcf60f4bSJie Wang MODULE_SOFTDEP("pre: crypto-intel_qat");
202fcf60f4bSJie Wang MODULE_IMPORT_NS(CRYPTO_QAT);
203