Path: blob/master/drivers/crypto/intel/qat/qat_c62x/adf_drv.c
29278 views
// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)1/* Copyright(c) 2014 - 2020 Intel Corporation */2#include <linux/kernel.h>3#include <linux/module.h>4#include <linux/pci.h>5#include <linux/init.h>6#include <linux/types.h>7#include <linux/fs.h>8#include <linux/slab.h>9#include <linux/errno.h>10#include <linux/device.h>11#include <linux/dma-mapping.h>12#include <linux/platform_device.h>13#include <linux/workqueue.h>14#include <linux/io.h>15#include <adf_accel_devices.h>16#include <adf_common_drv.h>17#include <adf_cfg.h>18#include <adf_dbgfs.h>19#include "adf_c62x_hw_data.h"2021static void adf_cleanup_pci_dev(struct adf_accel_dev *accel_dev)22{23pci_release_regions(accel_dev->accel_pci_dev.pci_dev);24pci_disable_device(accel_dev->accel_pci_dev.pci_dev);25}2627static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)28{29struct adf_accel_pci *accel_pci_dev = &accel_dev->accel_pci_dev;30int i;3132for (i = 0; i < ADF_PCI_MAX_BARS; i++) {33struct adf_bar *bar = &accel_pci_dev->pci_bars[i];3435if (bar->virt_addr)36pci_iounmap(accel_pci_dev->pci_dev, bar->virt_addr);37}3839if (accel_dev->hw_device) {40switch (accel_pci_dev->pci_dev->device) {41case PCI_DEVICE_ID_INTEL_QAT_C62X:42adf_clean_hw_data_c62x(accel_dev->hw_device);43break;44default:45break;46}47kfree(accel_dev->hw_device);48accel_dev->hw_device = NULL;49}50adf_dbgfs_exit(accel_dev);51adf_cfg_dev_remove(accel_dev);52adf_devmgr_rm_dev(accel_dev, NULL);53}5455static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)56{57struct adf_accel_dev *accel_dev;58struct adf_accel_pci *accel_pci_dev;59struct adf_hw_device_data *hw_data;60unsigned int i, bar_nr;61unsigned long bar_mask;62int ret;6364switch (ent->device) {65case PCI_DEVICE_ID_INTEL_QAT_C62X:66break;67default:68dev_err(&pdev->dev, "Invalid device 0x%x.\n", ent->device);69return -ENODEV;70}7172if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {73/* If the accelerator is connected to a node with no memory74* there is no point in using the accelerator since the remote75* memory transaction will be very slow. */76dev_err(&pdev->dev, "Invalid NUMA configuration.\n");77return -EINVAL;78}7980accel_dev = kzalloc_node(sizeof(*accel_dev), GFP_KERNEL,81dev_to_node(&pdev->dev));82if (!accel_dev)83return -ENOMEM;8485INIT_LIST_HEAD(&accel_dev->crypto_list);86accel_pci_dev = &accel_dev->accel_pci_dev;87accel_pci_dev->pci_dev = pdev;8889/* Add accel device to accel table.90* This should be called before adf_cleanup_accel is called */91if (adf_devmgr_add_dev(accel_dev, NULL)) {92dev_err(&pdev->dev, "Failed to add new accelerator device.\n");93kfree(accel_dev);94return -EFAULT;95}9697accel_dev->owner = THIS_MODULE;98/* Allocate and configure device configuration structure */99hw_data = kzalloc_node(sizeof(*hw_data), GFP_KERNEL,100dev_to_node(&pdev->dev));101if (!hw_data) {102ret = -ENOMEM;103goto out_err;104}105106accel_dev->hw_device = hw_data;107adf_init_hw_data_c62x(accel_dev->hw_device);108pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid);109pci_read_config_dword(pdev, ADF_DEVICE_FUSECTL_OFFSET,110&hw_data->fuses[ADF_FUSECTL0]);111pci_read_config_dword(pdev, ADF_C62X_SOFTSTRAP_CSR_OFFSET,112&hw_data->straps);113114/* Get Accelerators and Accelerators Engines masks */115hw_data->accel_mask = hw_data->get_accel_mask(hw_data);116hw_data->ae_mask = hw_data->get_ae_mask(hw_data);117accel_pci_dev->sku = hw_data->get_sku(hw_data);118/* If the device has no acceleration engines then ignore it. */119if (!hw_data->accel_mask || !hw_data->ae_mask ||120((~hw_data->ae_mask) & 0x01)) {121dev_err(&pdev->dev, "No acceleration units found");122ret = -EFAULT;123goto out_err;124}125126/* Create device configuration table */127ret = adf_cfg_dev_add(accel_dev);128if (ret)129goto out_err;130131/* enable PCI device */132if (pci_enable_device(pdev)) {133ret = -EFAULT;134goto out_err;135}136137/* set dma identifier */138ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));139if (ret) {140dev_err(&pdev->dev, "No usable DMA configuration\n");141goto out_err_disable;142}143144if (pci_request_regions(pdev, ADF_C62X_DEVICE_NAME)) {145ret = -EFAULT;146goto out_err_disable;147}148149/* Get accelerator capabilities mask */150hw_data->accel_capabilities_mask = hw_data->get_accel_cap(accel_dev);151152/* Find and map all the device's BARS */153i = (hw_data->fuses[ADF_FUSECTL0] & ADF_DEVICE_FUSECTL_MASK) ? 1 : 0;154bar_mask = pci_select_bars(pdev, IORESOURCE_MEM);155for_each_set_bit(bar_nr, &bar_mask, ADF_PCI_MAX_BARS * 2) {156struct adf_bar *bar = &accel_pci_dev->pci_bars[i++];157158bar->base_addr = pci_resource_start(pdev, bar_nr);159if (!bar->base_addr)160break;161bar->size = pci_resource_len(pdev, bar_nr);162bar->virt_addr = pci_iomap(accel_pci_dev->pci_dev, bar_nr, 0);163if (!bar->virt_addr) {164dev_err(&pdev->dev, "Failed to map BAR %d\n", bar_nr);165ret = -EFAULT;166goto out_err_free_reg;167}168}169pci_set_master(pdev);170171if (pci_save_state(pdev)) {172dev_err(&pdev->dev, "Failed to save pci state\n");173ret = -ENOMEM;174goto out_err_free_reg;175}176177adf_dbgfs_init(accel_dev);178179ret = adf_dev_up(accel_dev, true);180if (ret)181goto out_err_dev_stop;182183return ret;184185out_err_dev_stop:186adf_dev_down(accel_dev);187out_err_free_reg:188pci_release_regions(accel_pci_dev->pci_dev);189out_err_disable:190pci_disable_device(accel_pci_dev->pci_dev);191out_err:192adf_cleanup_accel(accel_dev);193kfree(accel_dev);194return ret;195}196197static void adf_remove(struct pci_dev *pdev)198{199struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);200201if (!accel_dev) {202pr_err("QAT: Driver removal failed\n");203return;204}205adf_dev_down(accel_dev);206adf_cleanup_accel(accel_dev);207adf_cleanup_pci_dev(accel_dev);208kfree(accel_dev);209}210211static void adf_shutdown(struct pci_dev *pdev)212{213struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);214215adf_dev_down(accel_dev);216}217218static const struct pci_device_id adf_pci_tbl[] = {219{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_C62X) },220{ }221};222MODULE_DEVICE_TABLE(pci, adf_pci_tbl);223224static struct pci_driver adf_driver = {225.id_table = adf_pci_tbl,226.name = ADF_C62X_DEVICE_NAME,227.probe = adf_probe,228.remove = adf_remove,229.shutdown = adf_shutdown,230.sriov_configure = adf_sriov_configure,231.err_handler = &adf_err_handler,232};233234static int __init adfdrv_init(void)235{236request_module("intel_qat");237238if (pci_register_driver(&adf_driver)) {239pr_err("QAT: Driver initialization failed\n");240return -EFAULT;241}242return 0;243}244245static void __exit adfdrv_release(void)246{247pci_unregister_driver(&adf_driver);248}249250module_init(adfdrv_init);251module_exit(adfdrv_release);252253MODULE_LICENSE("Dual BSD/GPL");254MODULE_AUTHOR("Intel");255MODULE_FIRMWARE(ADF_C62X_FW);256MODULE_FIRMWARE(ADF_C62X_MMP);257MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");258MODULE_VERSION(ADF_DRV_VERSION);259MODULE_IMPORT_NS("CRYPTO_QAT");260261262