1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 #include <sys/types.h>
5 #include <sys/sysctl.h>
6 #include <sys/systm.h>
7 #include "adf_heartbeat_dbg.h"
8 #include "adf_common_drv.h"
9 #include "adf_cfg.h"
10 #include "adf_heartbeat.h"
11 
12 #define HB_SYSCTL_ERR(RC)                                                           \
13 	do {                                                                        \
14 		if (RC == NULL) {                                                   \
15 			printf(                                                     \
16 			    "Memory allocation failed in adf_heartbeat_dbg_add\n"); \
17 			return ENOMEM;                                              \
18 		}                                                                   \
19 	} while (0)
20 
21 /* Handler for HB status check */
22 static int qat_dev_hb_read(SYSCTL_HANDLER_ARGS)
23 {
24 	enum adf_device_heartbeat_status hb_status = DEV_HB_UNRESPONSIVE;
25 	struct adf_accel_dev *accel_dev = arg1;
26 	struct adf_heartbeat *hb;
27 	int ret = 0;
28 	if (accel_dev == NULL) {
29 		return EINVAL;
30 	}
31 	hb = accel_dev->heartbeat;
32 
33 	/* if FW is loaded, proceed else set heartbeat down */
34 	if (test_bit(ADF_STATUS_AE_UCODE_LOADED, &accel_dev->status)) {
35 		adf_heartbeat_status(accel_dev, &hb_status);
36 	}
37 	if (hb_status == DEV_HB_ALIVE) {
38 		hb->heartbeat.hb_sysctlvar = 1;
39 	} else {
40 		hb->heartbeat.hb_sysctlvar = 0;
41 	}
42 	ret = sysctl_handle_int(oidp, &hb->heartbeat.hb_sysctlvar, 0, req);
43 	return ret;
44 }
45 
46 int
47 adf_heartbeat_dbg_add(struct adf_accel_dev *accel_dev)
48 {
49 	struct sysctl_ctx_list *qat_hb_sysctl_ctx;
50 	struct sysctl_oid *qat_hb_sysctl_tree;
51 	struct adf_heartbeat *hb;
52 	struct sysctl_oid *rc = 0;
53 
54 	if (accel_dev == NULL) {
55 		return EINVAL;
56 	}
57 
58 	if (adf_heartbeat_init(accel_dev))
59 		return EINVAL;
60 
61 	hb = accel_dev->heartbeat;
62 	qat_hb_sysctl_ctx =
63 	    device_get_sysctl_ctx(accel_dev->accel_pci_dev.pci_dev);
64 	qat_hb_sysctl_tree =
65 	    device_get_sysctl_tree(accel_dev->accel_pci_dev.pci_dev);
66 
67 	rc = SYSCTL_ADD_UINT(qat_hb_sysctl_ctx,
68 			     SYSCTL_CHILDREN(qat_hb_sysctl_tree),
69 			     OID_AUTO,
70 			     "heartbeat_sent",
71 			     CTLFLAG_RD,
72 			     &hb->hb_sent_counter,
73 			     0,
74 			     "HB sent count");
75 	HB_SYSCTL_ERR(rc);
76 
77 	rc = SYSCTL_ADD_UINT(qat_hb_sysctl_ctx,
78 			     SYSCTL_CHILDREN(qat_hb_sysctl_tree),
79 			     OID_AUTO,
80 			     "heartbeat_failed",
81 			     CTLFLAG_RD,
82 			     &hb->hb_failed_counter,
83 			     0,
84 			     "HB failed count");
85 	HB_SYSCTL_ERR(rc);
86 
87 	rc = SYSCTL_ADD_PROC(qat_hb_sysctl_ctx,
88 			     SYSCTL_CHILDREN(qat_hb_sysctl_tree),
89 			     OID_AUTO,
90 			     "heartbeat",
91 			     CTLTYPE_INT | CTLFLAG_RD,
92 			     accel_dev,
93 			     0,
94 			     qat_dev_hb_read,
95 			     "IU",
96 			     "QAT device status");
97 	HB_SYSCTL_ERR(rc);
98 	return 0;
99 }
100 
101 int
102 adf_heartbeat_dbg_del(struct adf_accel_dev *accel_dev)
103 {
104 	adf_heartbeat_clean(accel_dev);
105 	return 0;
106 }
107