File Index
Files and directories
Sign in
FILE

adf_aer.c

File preview with syntax highlighting.

linux-6.19.14/drivers/crypto/intel/qat/qat_common/adf_aer.c C
  1// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
  2/* Copyright(c) 2014 - 2020 Intel Corporation */
  3#include <linux/kernel.h>
  4#include <linux/pci.h>
  5#include <linux/completion.h>
  6#include <linux/workqueue.h>
  7#include <linux/delay.h>
  8#include "adf_accel_devices.h"
  9#include "adf_common_drv.h"
 10#include "adf_pfvf_pf_msg.h"
 11
 12struct adf_fatal_error_data {
 13	struct adf_accel_dev *accel_dev;
 14	struct work_struct work;
 15};
 16
 17static struct workqueue_struct *device_reset_wq;
 18static struct workqueue_struct *device_sriov_wq;
 19
 20static pci_ers_result_t adf_error_detected(struct pci_dev *pdev,
 21					   pci_channel_state_t state)
 22{
 23	struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
 24
 25	dev_info(&pdev->dev, "Acceleration driver hardware error detected.\n");
 26	if (!accel_dev) {
 27		dev_err(&pdev->dev, "Can't find acceleration device\n");
 28		return PCI_ERS_RESULT_DISCONNECT;
 29	}
 30
 31	if (state == pci_channel_io_perm_failure) {
 32		dev_err(&pdev->dev, "Can't recover from device error\n");
 33		return PCI_ERS_RESULT_DISCONNECT;
 34	}
 35
 36	set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
 37	if (accel_dev->hw_device->exit_arb) {
 38		dev_dbg(&pdev->dev, "Disabling arbitration\n");
 39		accel_dev->hw_device->exit_arb(accel_dev);
 40	}
 41	adf_error_notifier(accel_dev);
 42	adf_pf2vf_notify_fatal_error(accel_dev);
 43	adf_dev_restarting_notify(accel_dev);
 44	pci_clear_master(pdev);
 45	adf_dev_down(accel_dev);
 46
 47	return PCI_ERS_RESULT_NEED_RESET;
 48}
 49
 50/* reset dev data */
 51struct adf_reset_dev_data {
 52	int mode;
 53	struct adf_accel_dev *accel_dev;
 54	struct completion compl;
 55	struct work_struct reset_work;
 56};
 57
 58/* sriov dev data */
 59struct adf_sriov_dev_data {
 60	struct adf_accel_dev *accel_dev;
 61	struct completion compl;
 62	struct work_struct sriov_work;
 63};
 64
 65void adf_reset_sbr(struct adf_accel_dev *accel_dev)
 66{
 67	struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
 68	struct pci_dev *parent = pdev->bus->self;
 69	u16 bridge_ctl = 0;
 70
 71	if (!parent)
 72		parent = pdev;
 73
 74	if (!pci_wait_for_pending_transaction(pdev))
 75		dev_info(&GET_DEV(accel_dev),
 76			 "Transaction still in progress. Proceeding\n");
 77
 78	dev_info(&GET_DEV(accel_dev), "Secondary bus reset\n");
 79
 80	pci_read_config_word(parent, PCI_BRIDGE_CONTROL, &bridge_ctl);
 81	bridge_ctl |= PCI_BRIDGE_CTL_BUS_RESET;
 82	pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
 83	msleep(100);
 84	bridge_ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
 85	pci_write_config_word(parent, PCI_BRIDGE_CONTROL, bridge_ctl);
 86	msleep(100);
 87}
 88EXPORT_SYMBOL_GPL(adf_reset_sbr);
 89
 90void adf_reset_flr(struct adf_accel_dev *accel_dev)
 91{
 92	pcie_flr(accel_to_pci_dev(accel_dev));
 93}
 94EXPORT_SYMBOL_GPL(adf_reset_flr);
 95
 96void adf_dev_restore(struct adf_accel_dev *accel_dev)
 97{
 98	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
 99	struct pci_dev *pdev = accel_to_pci_dev(accel_dev);
100
101	if (hw_device->reset_device) {
102		dev_info(&GET_DEV(accel_dev), "Resetting device qat_dev%d\n",
103			 accel_dev->accel_id);
104		hw_device->reset_device(accel_dev);
105		pci_restore_state(pdev);
106	}
107}
108
109static void adf_device_sriov_worker(struct work_struct *work)
110{
111	struct adf_sriov_dev_data *sriov_data =
112		container_of(work, struct adf_sriov_dev_data, sriov_work);
113
114	adf_reenable_sriov(sriov_data->accel_dev);
115	complete(&sriov_data->compl);
116}
117
118static void adf_device_reset_worker(struct work_struct *work)
119{
120	struct adf_reset_dev_data *reset_data =
121		  container_of(work, struct adf_reset_dev_data, reset_work);
122	struct adf_accel_dev *accel_dev = reset_data->accel_dev;
123	unsigned long wait_jiffies = msecs_to_jiffies(10000);
124	struct adf_sriov_dev_data sriov_data;
125
126	adf_dev_restarting_notify(accel_dev);
127	if (adf_dev_restart(accel_dev)) {
128		/* The device hanged and we can't restart it so stop here */
129		dev_err(&GET_DEV(accel_dev), "Restart device failed\n");
130		if (reset_data->mode == ADF_DEV_RESET_ASYNC)
131			kfree(reset_data);
132		WARN(1, "QAT: device restart failed. Device is unusable\n");
133		return;
134	}
135
136	sriov_data.accel_dev = accel_dev;
137	init_completion(&sriov_data.compl);
138	INIT_WORK(&sriov_data.sriov_work, adf_device_sriov_worker);
139	queue_work(device_sriov_wq, &sriov_data.sriov_work);
140	if (wait_for_completion_timeout(&sriov_data.compl, wait_jiffies))
141		adf_pf2vf_notify_restarted(accel_dev);
142
143	adf_dev_restarted_notify(accel_dev);
144	clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
145
146	/* The dev is back alive. Notify the caller if in sync mode */
147	if (reset_data->mode == ADF_DEV_RESET_ASYNC)
148		kfree(reset_data);
149	else
150		complete(&reset_data->compl);
151}
152
153static int adf_dev_aer_schedule_reset(struct adf_accel_dev *accel_dev,
154				      enum adf_dev_reset_mode mode)
155{
156	struct adf_reset_dev_data *reset_data;
157
158	if (!adf_dev_started(accel_dev) ||
159	    test_bit(ADF_STATUS_RESTARTING, &accel_dev->status))
160		return 0;
161
162	set_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
163	reset_data = kzalloc(sizeof(*reset_data), GFP_KERNEL);
164	if (!reset_data)
165		return -ENOMEM;
166	reset_data->accel_dev = accel_dev;
167	init_completion(&reset_data->compl);
168	reset_data->mode = mode;
169	INIT_WORK(&reset_data->reset_work, adf_device_reset_worker);
170	queue_work(device_reset_wq, &reset_data->reset_work);
171
172	/* If in sync mode wait for the result */
173	if (mode == ADF_DEV_RESET_SYNC) {
174		int ret = 0;
175		/* Maximum device reset time is 10 seconds */
176		unsigned long wait_jiffies = msecs_to_jiffies(10000);
177		unsigned long timeout = wait_for_completion_timeout(
178				   &reset_data->compl, wait_jiffies);
179		if (!timeout) {
180			dev_err(&GET_DEV(accel_dev),
181				"Reset device timeout expired\n");
182			cancel_work_sync(&reset_data->reset_work);
183			ret = -EFAULT;
184		}
185		kfree(reset_data);
186		return ret;
187	}
188	return 0;
189}
190
191static pci_ers_result_t adf_slot_reset(struct pci_dev *pdev)
192{
193	struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev);
194	int res = 0;
195
196	if (!accel_dev) {
197		pr_err("QAT: Can't find acceleration device\n");
198		return PCI_ERS_RESULT_DISCONNECT;
199	}
200
201	if (!pdev->is_busmaster)
202		pci_set_master(pdev);
203	pci_restore_state(pdev);
204	res = adf_dev_up(accel_dev, false);
205	if (res && res != -EALREADY)
206		return PCI_ERS_RESULT_DISCONNECT;
207
208	adf_reenable_sriov(accel_dev);
209	adf_pf2vf_notify_restarted(accel_dev);
210	adf_dev_restarted_notify(accel_dev);
211	clear_bit(ADF_STATUS_RESTARTING, &accel_dev->status);
212	return PCI_ERS_RESULT_RECOVERED;
213}
214
215static void adf_resume(struct pci_dev *pdev)
216{
217	dev_info(&pdev->dev, "Acceleration driver reset completed\n");
218	dev_info(&pdev->dev, "Device is up and running\n");
219}
220
221const struct pci_error_handlers adf_err_handler = {
222	.error_detected = adf_error_detected,
223	.slot_reset = adf_slot_reset,
224	.resume = adf_resume,
225};
226EXPORT_SYMBOL_GPL(adf_err_handler);
227
228static int adf_dev_autoreset(struct adf_accel_dev *accel_dev)
229{
230	if (accel_dev->autoreset_on_error)
231		return adf_dev_aer_schedule_reset(accel_dev, ADF_DEV_RESET_ASYNC);
232
233	return 0;
234}
235
236static void adf_notify_fatal_error_worker(struct work_struct *work)
237{
238	struct adf_fatal_error_data *wq_data =
239			container_of(work, struct adf_fatal_error_data, work);
240	struct adf_accel_dev *accel_dev = wq_data->accel_dev;
241	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
242
243	adf_error_notifier(accel_dev);
244
245	if (!accel_dev->is_vf) {
246		/* Disable arbitration to stop processing of new requests */
247		if (accel_dev->autoreset_on_error && hw_device->exit_arb)
248			hw_device->exit_arb(accel_dev);
249		if (accel_dev->pf.vf_info)
250			adf_pf2vf_notify_fatal_error(accel_dev);
251		adf_dev_autoreset(accel_dev);
252	}
253
254	kfree(wq_data);
255}
256
257int adf_notify_fatal_error(struct adf_accel_dev *accel_dev)
258{
259	struct adf_fatal_error_data *wq_data;
260
261	wq_data = kzalloc(sizeof(*wq_data), GFP_ATOMIC);
262	if (!wq_data)
263		return -ENOMEM;
264
265	wq_data->accel_dev = accel_dev;
266	INIT_WORK(&wq_data->work, adf_notify_fatal_error_worker);
267	adf_misc_wq_queue_work(&wq_data->work);
268
269	return 0;
270}
271
272int adf_init_aer(void)
273{
274	device_reset_wq = alloc_workqueue("qat_device_reset_wq",
275					  WQ_MEM_RECLAIM | WQ_PERCPU, 0);
276	if (!device_reset_wq)
277		return -EFAULT;
278
279	device_sriov_wq = alloc_workqueue("qat_device_sriov_wq", WQ_PERCPU, 0);
280	if (!device_sriov_wq) {
281		destroy_workqueue(device_reset_wq);
282		device_reset_wq = NULL;
283		return -EFAULT;
284	}
285
286	return 0;
287}
288
289void adf_exit_aer(void)
290{
291	if (device_reset_wq)
292		destroy_workqueue(device_reset_wq);
293	device_reset_wq = NULL;
294
295	if (device_sriov_wq)
296		destroy_workqueue(device_sriov_wq);
297	device_sriov_wq = NULL;
298}
~/ Read-only file preview.