aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/qat/qat_common/adf_freebsd_heartbeat_dbg.c
blob: e7b4840600e1626c14450bb36da84b036480f946 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* SPDX-License-Identifier: BSD-3-Clause */
/* Copyright(c) 2007-2025 Intel Corporation */
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/priv.h>
#include "adf_heartbeat_dbg.h"
#include "adf_common_drv.h"
#include "adf_cfg.h"
#include "adf_heartbeat.h"

#define HB_SYSCTL_ERR(RC)                                                           \
	do {                                                                        \
		if (RC == NULL) {                                                   \
			printf(                                                     \
			    "Memory allocation failed in adf_heartbeat_dbg_add\n"); \
			return ENOMEM;                                              \
		}                                                                   \
	} while (0)


static int qat_dev_hb_read_sent(SYSCTL_HANDLER_ARGS)
{
	struct adf_accel_dev *accel_dev = arg1;
	struct adf_heartbeat *hb;
	int error = EFAULT;

	if (priv_check(curthread, PRIV_DRIVER) != 0)
		return EPERM;

	if (accel_dev == NULL)
		return EINVAL;

	hb = accel_dev->heartbeat;

	error = sysctl_handle_int(oidp, &hb->hb_sent_counter, 0, req);
	if (error || !req->newptr)
		return error;

	return (0);
}

static int qat_dev_hb_read_failed(SYSCTL_HANDLER_ARGS)
{
	struct adf_accel_dev *accel_dev = arg1;
	struct adf_heartbeat *hb;
	int error = EFAULT;

	if (priv_check(curthread, PRIV_DRIVER) != 0)
		return EPERM;

	if (accel_dev == NULL)
		return EINVAL;

	hb = accel_dev->heartbeat;

	error = sysctl_handle_int(oidp, &hb->hb_failed_counter, 0, req);
	if (error || !req->newptr)
		return error;

	return (0);
}

/* Handler for HB status check */
static int qat_dev_hb_read(SYSCTL_HANDLER_ARGS)
{
	enum adf_device_heartbeat_status hb_status = DEV_HB_UNRESPONSIVE;
	struct adf_accel_dev *accel_dev = arg1;
	struct adf_heartbeat *hb;
	int ret = 0;

	if (priv_check(curthread, PRIV_DRIVER) != 0)
		return EPERM;

	if (accel_dev == NULL) {
		return EINVAL;
	}
	hb = accel_dev->heartbeat;

	/* if FW is loaded, proceed else set heartbeat down */
	if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
		adf_heartbeat_status(accel_dev, &hb_status);
	}
	if (hb_status == DEV_HB_ALIVE) {
		hb->heartbeat.hb_sysctlvar = 1;
	} else {
		hb->heartbeat.hb_sysctlvar = 0;
	}
	ret = sysctl_handle_int(oidp, &hb->heartbeat.hb_sysctlvar, 0, req);
	return ret;
}

int
adf_heartbeat_dbg_add(struct adf_accel_dev *accel_dev)
{
	struct sysctl_ctx_list *qat_hb_sysctl_ctx;
	struct sysctl_oid *qat_hb_sysctl_tree;
	struct adf_heartbeat *hb;

	if (accel_dev == NULL) {
		return EINVAL;
	}

	if (adf_heartbeat_init(accel_dev))
		return EINVAL;

	hb = accel_dev->heartbeat;
	qat_hb_sysctl_ctx =
	    device_get_sysctl_ctx(accel_dev->accel_pci_dev.pci_dev);
	qat_hb_sysctl_tree =
	    device_get_sysctl_tree(accel_dev->accel_pci_dev.pci_dev);

	hb->heartbeat_sent.oid =
	    SYSCTL_ADD_PROC(qat_hb_sysctl_ctx,
			    SYSCTL_CHILDREN(qat_hb_sysctl_tree),
			    OID_AUTO,
			    "heartbeat_sent",
			    CTLTYPE_INT | CTLFLAG_RD,
			    accel_dev,
			    0,
			    qat_dev_hb_read_sent,
			    "IU",
			    "HB failed count");
	HB_SYSCTL_ERR(hb->heartbeat_sent.oid);

	hb->heartbeat_failed.oid =
	    SYSCTL_ADD_PROC(qat_hb_sysctl_ctx,
			    SYSCTL_CHILDREN(qat_hb_sysctl_tree),
			    OID_AUTO,
			    "heartbeat_failed",
			    CTLTYPE_INT | CTLFLAG_RD,
			    accel_dev,
			    0,
			    qat_dev_hb_read_failed,
			    "IU",
			    "HB failed count");
	HB_SYSCTL_ERR(hb->heartbeat_failed.oid);

	hb->heartbeat.oid = SYSCTL_ADD_PROC(qat_hb_sysctl_ctx,
					    SYSCTL_CHILDREN(qat_hb_sysctl_tree),
					    OID_AUTO,
					    "heartbeat",
					    CTLTYPE_INT | CTLFLAG_RD,
					    accel_dev,
					    0,
					    qat_dev_hb_read,
					    "IU",
					    "QAT device status");
	HB_SYSCTL_ERR(hb->heartbeat.oid);
	return 0;
}

int
adf_heartbeat_dbg_del(struct adf_accel_dev *accel_dev)
{
	struct sysctl_ctx_list *qat_sysctl_ctx;
	struct adf_heartbeat *hb;

	if (!accel_dev) {
		return EINVAL;
	}

	hb = accel_dev->heartbeat;

	qat_sysctl_ctx =
	    device_get_sysctl_ctx(accel_dev->accel_pci_dev.pci_dev);

	if (hb->heartbeat.oid) {
		sysctl_ctx_entry_del(qat_sysctl_ctx, hb->heartbeat.oid);
		sysctl_remove_oid(hb->heartbeat.oid, 1, 1);
		hb->heartbeat.oid = NULL;
	}

	if (hb->heartbeat_failed.oid) {
		sysctl_ctx_entry_del(qat_sysctl_ctx, hb->heartbeat_failed.oid);
		sysctl_remove_oid(hb->heartbeat_failed.oid, 1, 1);
		hb->heartbeat_failed.oid = NULL;
	}

	if (hb->heartbeat_sent.oid) {
		sysctl_ctx_entry_del(qat_sysctl_ctx, hb->heartbeat_sent.oid);
		sysctl_remove_oid(hb->heartbeat_sent.oid, 1, 1);
		hb->heartbeat_sent.oid = NULL;
	}

	adf_heartbeat_clean(accel_dev);

	return 0;
}