Path: blob/master/drivers/crypto/intel/qat/qat_4xxx/adf_drv.c
29278 views
// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)1/* Copyright(c) 2020 Intel Corporation */2#include <linux/device.h>3#include <linux/module.h>4#include <linux/pci.h>56#include <adf_accel_devices.h>7#include <adf_cfg.h>8#include <adf_common_drv.h>9#include <adf_dbgfs.h>10#include <adf_gen4_config.h>11#include <adf_gen4_hw_data.h>1213#include "adf_4xxx_hw_data.h"1415static const struct pci_device_id adf_pci_tbl[] = {16{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_4XXX) },17{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_401XX) },18{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_QAT_402XX) },19{ }20};21MODULE_DEVICE_TABLE(pci, adf_pci_tbl);2223static void adf_cleanup_accel(struct adf_accel_dev *accel_dev)24{25if (accel_dev->hw_device) {26adf_clean_hw_data_4xxx(accel_dev->hw_device);27accel_dev->hw_device = NULL;28}29adf_dbgfs_exit(accel_dev);30adf_cfg_dev_remove(accel_dev);31adf_devmgr_rm_dev(accel_dev, NULL);32}3334static int adf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)35{36struct adf_accel_dev *accel_dev;37struct adf_accel_pci *accel_pci_dev;38struct adf_hw_device_data *hw_data;39unsigned int i, bar_nr;40unsigned long bar_mask;41struct adf_bar *bar;42int ret;4344if (num_possible_nodes() > 1 && dev_to_node(&pdev->dev) < 0) {45/*46* If the accelerator is connected to a node with no memory47* there is no point in using the accelerator since the remote48* memory transaction will be very slow.49*/50dev_err(&pdev->dev, "Invalid NUMA configuration.\n");51return -EINVAL;52}5354accel_dev = devm_kzalloc(&pdev->dev, sizeof(*accel_dev), GFP_KERNEL);55if (!accel_dev)56return -ENOMEM;5758INIT_LIST_HEAD(&accel_dev->crypto_list);59accel_pci_dev = &accel_dev->accel_pci_dev;60accel_pci_dev->pci_dev = pdev;6162/*63* Add accel device to accel table64* This should be called before adf_cleanup_accel is called65*/66if (adf_devmgr_add_dev(accel_dev, NULL)) {67dev_err(&pdev->dev, "Failed to add new accelerator device.\n");68return -EFAULT;69}7071accel_dev->owner = THIS_MODULE;72/* Allocate and initialise device hardware meta-data structure */73hw_data = devm_kzalloc(&pdev->dev, sizeof(*hw_data), GFP_KERNEL);74if (!hw_data) {75ret = -ENOMEM;76goto out_err;77}7879accel_dev->hw_device = hw_data;80adf_init_hw_data_4xxx(accel_dev->hw_device, ent->device);8182pci_read_config_byte(pdev, PCI_REVISION_ID, &accel_pci_dev->revid);83pci_read_config_dword(pdev, ADF_GEN4_FUSECTL4_OFFSET, &hw_data->fuses[ADF_FUSECTL4]);8485/* Get Accelerators and Accelerators Engines masks */86hw_data->accel_mask = hw_data->get_accel_mask(hw_data);87hw_data->ae_mask = hw_data->get_ae_mask(hw_data);88accel_pci_dev->sku = hw_data->get_sku(hw_data);89/* If the device has no acceleration engines then ignore it */90if (!hw_data->accel_mask || !hw_data->ae_mask ||91(~hw_data->ae_mask & 0x01)) {92dev_err(&pdev->dev, "No acceleration units found.\n");93ret = -EFAULT;94goto out_err;95}9697/* Create device configuration table */98ret = adf_cfg_dev_add(accel_dev);99if (ret)100goto out_err;101102/* Enable PCI device */103ret = pcim_enable_device(pdev);104if (ret) {105dev_err(&pdev->dev, "Can't enable PCI device.\n");106goto out_err;107}108109/* Set DMA identifier */110ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));111if (ret) {112dev_err(&pdev->dev, "No usable DMA configuration.\n");113goto out_err;114}115116ret = adf_gen4_cfg_dev_init(accel_dev);117if (ret) {118dev_err(&pdev->dev, "Failed to initialize configuration.\n");119goto out_err;120}121122/* Get accelerator capabilities mask */123hw_data->accel_capabilities_mask = hw_data->get_accel_cap(accel_dev);124if (!hw_data->accel_capabilities_mask) {125dev_err(&pdev->dev, "Failed to get capabilities mask.\n");126ret = -EINVAL;127goto out_err;128}129130/* Find and map all the device's BARS */131bar_mask = pci_select_bars(pdev, IORESOURCE_MEM) & ADF_GEN4_BAR_MASK;132133ret = pcim_request_all_regions(pdev, pci_name(pdev));134if (ret) {135dev_err(&pdev->dev, "Failed to request PCI regions.\n");136goto out_err;137}138139i = 0;140for_each_set_bit(bar_nr, &bar_mask, PCI_STD_NUM_BARS) {141bar = &accel_pci_dev->pci_bars[i++];142bar->virt_addr = pcim_iomap(pdev, bar_nr, 0);143if (!bar->virt_addr) {144dev_err(&pdev->dev, "Failed to ioremap PCI region.\n");145ret = -ENOMEM;146goto out_err;147}148}149150pci_set_master(pdev);151152if (pci_save_state(pdev)) {153dev_err(&pdev->dev, "Failed to save pci state.\n");154ret = -ENOMEM;155goto out_err;156}157158accel_dev->ras_errors.enabled = true;159adf_dbgfs_init(accel_dev);160161ret = adf_dev_up(accel_dev, true);162if (ret)163goto out_err_dev_stop;164165ret = adf_sysfs_init(accel_dev);166if (ret)167goto out_err_dev_stop;168169return ret;170171out_err_dev_stop:172adf_dev_down(accel_dev);173out_err:174adf_cleanup_accel(accel_dev);175return ret;176}177178static void adf_remove(struct pci_dev *pdev)179{180struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);181182if (!accel_dev) {183pr_err("QAT: Driver removal failed\n");184return;185}186adf_dev_down(accel_dev);187adf_cleanup_accel(accel_dev);188}189190static void adf_shutdown(struct pci_dev *pdev)191{192struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);193194adf_dev_down(accel_dev);195}196197static struct pci_driver adf_driver = {198.id_table = adf_pci_tbl,199.name = ADF_4XXX_DEVICE_NAME,200.probe = adf_probe,201.remove = adf_remove,202.shutdown = adf_shutdown,203.sriov_configure = adf_sriov_configure,204.err_handler = &adf_err_handler,205};206207module_pci_driver(adf_driver);208209MODULE_LICENSE("Dual BSD/GPL");210MODULE_AUTHOR("Intel");211MODULE_FIRMWARE(ADF_4XXX_FW);212MODULE_FIRMWARE(ADF_402XX_FW);213MODULE_FIRMWARE(ADF_4XXX_MMP);214MODULE_FIRMWARE(ADF_402XX_MMP);215MODULE_DESCRIPTION("Intel(R) QuickAssist Technology");216MODULE_VERSION(ADF_DRV_VERSION);217MODULE_SOFTDEP("pre: crypto-intel_qat");218MODULE_IMPORT_NS("CRYPTO_QAT");219220221