xref: /linux/drivers/crypto/intel/qat/qat_common/adf_heartbeat_inject.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2023 Intel Corporation */
3 #include <linux/random.h>
4 
5 #include "adf_admin.h"
6 #include "adf_common_drv.h"
7 #include "adf_heartbeat.h"
8 
9 #define MAX_HB_TICKS 0xFFFFFFFF
10 
11 static int adf_hb_set_timer_to_max(struct adf_accel_dev *accel_dev)
12 {
13 	struct adf_hw_device_data *hw_data = accel_dev->hw_device;
14 
15 	accel_dev->heartbeat->hb_timer = 0;
16 
17 	if (hw_data->stop_timer)
18 		hw_data->stop_timer(accel_dev);
19 
20 	return adf_send_admin_hb_timer(accel_dev, MAX_HB_TICKS);
21 }
22 
23 static void adf_set_hb_counters_fail(struct adf_accel_dev *accel_dev, u32 ae,
24 				     u32 thr)
25 {
26 	struct hb_cnt_pair *stats = accel_dev->heartbeat->dma.virt_addr;
27 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
28 	const size_t max_aes = hw_device->get_num_aes(hw_device);
29 	const size_t hb_ctrs = hw_device->num_hb_ctrs;
30 	size_t thr_id = ae * hb_ctrs + thr;
31 	u16 num_rsp = stats[thr_id].resp_heartbeat_cnt;
32 
33 	/*
34 	 * Inject live.req != live.rsp and live.rsp == last.rsp
35 	 * to trigger the heartbeat error detection
36 	 */
37 	stats[thr_id].req_heartbeat_cnt++;
38 	stats += (max_aes * hb_ctrs);
39 	stats[thr_id].resp_heartbeat_cnt = num_rsp;
40 }
41 
42 int adf_heartbeat_inject_error(struct adf_accel_dev *accel_dev)
43 {
44 	struct adf_hw_device_data *hw_device = accel_dev->hw_device;
45 	const size_t max_aes = hw_device->get_num_aes(hw_device);
46 	const size_t hb_ctrs = hw_device->num_hb_ctrs;
47 	u32 rand, rand_ae, rand_thr;
48 	unsigned long ae_mask;
49 	int ret;
50 
51 	ae_mask = hw_device->ae_mask;
52 
53 	do {
54 		/* Ensure we have a valid ae */
55 		get_random_bytes(&rand, sizeof(rand));
56 		rand_ae = rand % max_aes;
57 	} while (!test_bit(rand_ae, &ae_mask));
58 
59 	get_random_bytes(&rand, sizeof(rand));
60 	rand_thr = rand % hb_ctrs;
61 
62 	/* Increase the heartbeat timer to prevent FW updating HB counters */
63 	ret = adf_hb_set_timer_to_max(accel_dev);
64 	if (ret)
65 		return ret;
66 
67 	/* Configure worker threads to stop processing any packet */
68 	ret = adf_disable_arb_thd(accel_dev, rand_ae, rand_thr);
69 	if (ret)
70 		return ret;
71 
72 	/* Change HB counters memory to simulate a hang */
73 	adf_set_hb_counters_fail(accel_dev, rand_ae, rand_thr);
74 
75 	return 0;
76 }
77