Path: blob/master/drivers/crypto/intel/qat/qat_common/adf_aer.c
52750 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/pci.h>4#include <linux/completion.h>5#include <linux/workqueue.h>6#include <linux/delay.h>7#include "adf_accel_devices.h"8#include "adf_common_drv.h"9#include "adf_pfvf_pf_msg.h"1011struct adf_fatal_error_data {12struct adf_accel_dev *accel_dev;13struct work_struct work;14};1516static struct workqueue_struct *device_reset_wq;17static struct workqueue_struct *device_sriov_wq;1819static pci_ers_result_t adf_error_detected(struct pci_dev *pdev,20pci_channel_state_t state)21{22struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);2324dev_info(&pdev->dev, "Acceleration driver hardware error detected.\n");25if (!accel_dev) {26dev_err(&pdev->dev, "Can't find acceleration device\n");27return PCI_ERS_RESULT_DISCONNECT;28}2930if (state == pci_channel_io_perm_failure) {31dev_err(&pdev->dev, "Can't recover from device error\n");32return PCI_ERS_RESULT_DISCONNECT;33}3435set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);36if (accel_dev->hw_device->exit_arb) {37dev_dbg(&pdev->dev, "Disabling arbitration\n");38accel_dev->hw_device->exit_arb(accel_dev);39}40adf_error_notifier(accel_dev);41adf_pf2vf_notify_fatal_error(accel_dev);42adf_dev_restarting_notify(accel_dev);43pci_clear_master(pdev);44adf_dev_down(accel_dev);4546return PCI_ERS_RESULT_NEED_RESET;47}4849/* reset dev data */50struct adf_reset_dev_data {51int mode;52struct adf_accel_dev *accel_dev;53struct completion compl;54struct work_struct reset_work;55};5657/* sriov dev data */58struct adf_sriov_dev_data {59struct adf_accel_dev *accel_dev;60struct completion compl;61struct work_struct sriov_work;62};6364void adf_reset_sbr(struct adf_accel_dev *accel_dev)65{66struct pci_dev *pdev = accel_to_pci_dev(accel_dev);67struct pci_dev *parent = pdev->bus->self;68u16 bridge_ctl = 0;6970if (!parent)71parent = pdev;7273if (!pci_wait_for_pending_transaction(pdev))74dev_info(&GET_DEV(accel_dev),75"Transaction still in progress. Proceeding\n");7677dev_info(&GET_DEV(accel_dev), "Secondary bus reset\n");7879pci_read_config_word(parent, PCI_BRIDGE_CONTROL, &bridge_ctl);80bridge_ctl |= PCI_BRIDGE_CTL_BUS_RESET;81pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);82msleep(100);83bridge_ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;84pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);85msleep(100);86}87EXPORT_SYMBOL_GPL(adf_reset_sbr);8889void adf_reset_flr(struct adf_accel_dev *accel_dev)90{91pcie_flr(accel_to_pci_dev(accel_dev));92}93EXPORT_SYMBOL_GPL(adf_reset_flr);9495void adf_dev_restore(struct adf_accel_dev *accel_dev)96{97struct adf_hw_device_data *hw_device = accel_dev->hw_device;98struct pci_dev *pdev = accel_to_pci_dev(accel_dev);99100if (hw_device->reset_device) {101dev_info(&GET_DEV(accel_dev), "Resetting device qat_dev%d\n",102accel_dev->accel_id);103hw_device->reset_device(accel_dev);104pci_restore_state(pdev);105}106}107108static void adf_device_sriov_worker(struct work_struct *work)109{110struct adf_sriov_dev_data *sriov_data =111container_of(work, struct adf_sriov_dev_data, sriov_work);112113adf_reenable_sriov(sriov_data->accel_dev);114complete(&sriov_data->compl);115}116117static void adf_device_reset_worker(struct work_struct *work)118{119struct adf_reset_dev_data *reset_data =120container_of(work, struct adf_reset_dev_data, reset_work);121struct adf_accel_dev *accel_dev = reset_data->accel_dev;122unsigned long wait_jiffies = msecs_to_jiffies(10000);123struct adf_sriov_dev_data sriov_data;124125adf_dev_restarting_notify(accel_dev);126if (adf_dev_restart(accel_dev)) {127/* The device hanged and we can't restart it so stop here */128dev_err(&GET_DEV(accel_dev), "Restart device failed\n");129if (reset_data->mode == ADF_DEV_RESET_ASYNC)130kfree(reset_data);131WARN(1, "QAT: device restart failed. Device is unusable\n");132return;133}134135sriov_data.accel_dev = accel_dev;136init_completion(&sriov_data.compl);137INIT_WORK(&sriov_data.sriov_work, adf_device_sriov_worker);138queue_work(device_sriov_wq, &sriov_data.sriov_work);139if (wait_for_completion_timeout(&sriov_data.compl, wait_jiffies))140adf_pf2vf_notify_restarted(accel_dev);141142adf_dev_restarted_notify(accel_dev);143clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);144145/* The dev is back alive. Notify the caller if in sync mode */146if (reset_data->mode == ADF_DEV_RESET_ASYNC)147kfree(reset_data);148else149complete(&reset_data->compl);150}151152static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,153enum adf_dev_reset_mode mode)154{155struct adf_reset_dev_data *reset_data;156157if (!adf_dev_started(accel_dev) ||158test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))159return 0;160161set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);162reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);163if (!reset_data)164return -ENOMEM;165reset_data->accel_dev = accel_dev;166init_completion(&reset_data->compl);167reset_data->mode = mode;168INIT_WORK(&reset_data->reset_work, adf_device_reset_worker);169queue_work(device_reset_wq, &reset_data->reset_work);170171/* If in sync mode wait for the result */172if (mode == ADF_DEV_RESET_SYNC) {173int ret = 0;174/* Maximum device reset time is 10 seconds */175unsigned long wait_jiffies = msecs_to_jiffies(10000);176unsigned long timeout = wait_for_completion_timeout(177&reset_data->compl, wait_jiffies);178if (!timeout) {179dev_err(&GET_DEV(accel_dev),180"Reset device timeout expired\n");181cancel_work_sync(&reset_data->reset_work);182ret = -EFAULT;183}184kfree(reset_data);185return ret;186}187return 0;188}189190static pci_ers_result_t adf_slot_reset(struct pci_dev *pdev)191{192struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);193int res = 0;194195if (!accel_dev) {196pr_err("QAT: Can't find acceleration device\n");197return PCI_ERS_RESULT_DISCONNECT;198}199200if (!pdev->is_busmaster)201pci_set_master(pdev);202pci_restore_state(pdev);203res = adf_dev_up(accel_dev, false);204if (res && res != -EALREADY)205return PCI_ERS_RESULT_DISCONNECT;206207adf_reenable_sriov(accel_dev);208adf_pf2vf_notify_restarted(accel_dev);209adf_dev_restarted_notify(accel_dev);210clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);211return PCI_ERS_RESULT_RECOVERED;212}213214static void adf_resume(struct pci_dev *pdev)215{216dev_info(&pdev->dev, "Acceleration driver reset completed\n");217dev_info(&pdev->dev, "Device is up and running\n");218}219220const struct pci_error_handlers adf_err_handler = {221.error_detected = adf_error_detected,222.slot_reset = adf_slot_reset,223.resume = adf_resume,224};225EXPORT_SYMBOL_GPL(adf_err_handler);226227static int adf_dev_autoreset(struct adf_accel_dev *accel_dev)228{229if (accel_dev->autoreset_on_error)230return adf_dev_aer_schedule_reset(accel_dev, ADF_DEV_RESET_ASYNC);231232return 0;233}234235static void adf_notify_fatal_error_worker(struct work_struct *work)236{237struct adf_fatal_error_data *wq_data =238container_of(work, struct adf_fatal_error_data, work);239struct adf_accel_dev *accel_dev = wq_data->accel_dev;240struct adf_hw_device_data *hw_device = accel_dev->hw_device;241242adf_error_notifier(accel_dev);243244if (!accel_dev->is_vf) {245/* Disable arbitration to stop processing of new requests */246if (accel_dev->autoreset_on_error && hw_device->exit_arb)247hw_device->exit_arb(accel_dev);248if (accel_dev->pf.vf_info)249adf_pf2vf_notify_fatal_error(accel_dev);250adf_dev_autoreset(accel_dev);251}252253kfree(wq_data);254}255256int adf_notify_fatal_error(struct adf_accel_dev *accel_dev)257{258struct adf_fatal_error_data *wq_data;259260wq_data = kzalloc(sizeof(*wq_data), GFP_ATOMIC);261if (!wq_data)262return -ENOMEM;263264wq_data->accel_dev = accel_dev;265INIT_WORK(&wq_data->work, adf_notify_fatal_error_worker);266adf_misc_wq_queue_work(&wq_data->work);267268return 0;269}270271int adf_init_aer(void)272{273device_reset_wq = alloc_workqueue("qat_device_reset_wq",274WQ_MEM_RECLAIM | WQ_PERCPU, 0);275if (!device_reset_wq)276return -EFAULT;277278device_sriov_wq = alloc_workqueue("qat_device_sriov_wq", WQ_PERCPU, 0);279if (!device_sriov_wq) {280destroy_workqueue(device_reset_wq);281device_reset_wq = NULL;282return -EFAULT;283}284285return 0;286}287288void adf_exit_aer(void)289{290if (device_reset_wq)291destroy_workqueue(device_reset_wq);292device_reset_wq = NULL;293294if (device_sriov_wq)295destroy_workqueue(device_sriov_wq);296device_sriov_wq = NULL;297}298299300