1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 /**
5  *****************************************************************************
6  * @file sal_statistics.c
7  *
8  * @defgroup SalStats  Sal Statistics
9  *
10  * @ingroup SalStats
11  *
12  * @description
13  *    This file contains implementation of statistic related functions
14  *
15  *****************************************************************************/
16 
17 #include "cpa.h"
18 #include "lac_common.h"
19 #include "lac_mem.h"
20 #include "icp_adf_cfg.h"
21 #include "icp_accel_devices.h"
22 #include "sal_statistics.h"
23 
24 #include "icp_adf_debug.h"
25 #include "lac_sal_types.h"
26 #include "lac_sal.h"
27 
28 /**
29  ******************************************************************************
30  * @ingroup SalStats
31  *      Reads from the config file if the given statistic is enabled
32  *
33  * @description
34  *      Reads from the config file if the given statistic is enabled
35  *
36  * @param[in]  device           Pointer to an acceleration device structure
37  * @param[in]  statsName        Name of the config value to read the value from
38  * @param[out] pIsEnabled       Pointer to a variable where information if the
39  *                              given stat is enabled or disabled will be stored
40  *
41  * @retval  CPA_STATUS_SUCCESS          Operation successful
42  * @retval  CPA_STATUS_INVALID_PARAM    Invalid param provided
43  * @retval  CPA_STATUS_FAIL             Operation failed
44  *
45  ******************************************************************************/
46 static CpaStatus
47 SalStatistics_GetStatEnabled(icp_accel_dev_t *device,
48 			     const char *statsName,
49 			     CpaBoolean *pIsEnabled)
50 {
51 	CpaStatus status = CPA_STATUS_SUCCESS;
52 	char param_value[ADF_CFG_MAX_VAL_LEN_IN_BYTES] = { 0 };
53 
54 	LAC_CHECK_NULL_PARAM(pIsEnabled);
55 	LAC_CHECK_NULL_PARAM(statsName);
56 
57 	status = icp_adf_cfgGetParamValue(device,
58 					  LAC_CFG_SECTION_GENERAL,
59 					  statsName,
60 					  param_value);
61 
62 	if (CPA_STATUS_SUCCESS != status) {
63 		QAT_UTILS_LOG("Failed to get %s from configuration.\n",
64 			      statsName);
65 		return status;
66 	}
67 
68 	if (0 == strncmp(param_value,
69 			 SAL_STATISTICS_STRING_OFF,
70 			 strlen(SAL_STATISTICS_STRING_OFF))) {
71 		*pIsEnabled = CPA_FALSE;
72 	} else {
73 		*pIsEnabled = CPA_TRUE;
74 	}
75 
76 	return status;
77 }
78 
79 /* @ingroup SalStats */
80 CpaStatus
81 SalStatistics_InitStatisticsCollection(icp_accel_dev_t *device)
82 {
83 	CpaStatus status = CPA_STATUS_SUCCESS;
84 	sal_statistics_collection_t *pStatsCollection = NULL;
85 	Cpa32U enabled_services = 0;
86 
87 	LAC_CHECK_NULL_PARAM(device);
88 
89 	pStatsCollection = LAC_OS_MALLOC(sizeof(sal_statistics_collection_t));
90 	if (NULL == pStatsCollection) {
91 		QAT_UTILS_LOG("Failed to allocate memory for statistic.\n");
92 		return CPA_STATUS_RESOURCE;
93 	}
94 	device->pQatStats = pStatsCollection;
95 
96 	status = SalStatistics_GetStatEnabled(device,
97 					      SAL_STATS_CFG_ENABLED,
98 					      &pStatsCollection->bStatsEnabled);
99 	LAC_CHECK_STATUS(status);
100 
101 	if (CPA_FALSE == pStatsCollection->bStatsEnabled) {
102 		pStatsCollection->bDcStatsEnabled = CPA_FALSE;
103 		pStatsCollection->bDhStatsEnabled = CPA_FALSE;
104 		pStatsCollection->bDsaStatsEnabled = CPA_FALSE;
105 		pStatsCollection->bEccStatsEnabled = CPA_FALSE;
106 		pStatsCollection->bKeyGenStatsEnabled = CPA_FALSE;
107 		pStatsCollection->bLnStatsEnabled = CPA_FALSE;
108 		pStatsCollection->bPrimeStatsEnabled = CPA_FALSE;
109 		pStatsCollection->bRsaStatsEnabled = CPA_FALSE;
110 		pStatsCollection->bSymStatsEnabled = CPA_FALSE;
111 
112 		return status;
113 	}
114 
115 	/* What services are enabled */
116 	status = SalCtrl_GetEnabledServices(device, &enabled_services);
117 	if (CPA_STATUS_SUCCESS != status) {
118 		QAT_UTILS_LOG("Failed to get enabled services.\n");
119 		return CPA_STATUS_FAIL;
120 	}
121 
122 	/* Check if the compression service is enabled */
123 	if (SalCtrl_IsServiceEnabled(enabled_services,
124 				     SAL_SERVICE_TYPE_COMPRESSION)) {
125 		status = SalStatistics_GetStatEnabled(
126 		    device,
127 		    SAL_STATS_CFG_DC,
128 		    &pStatsCollection->bDcStatsEnabled);
129 		LAC_CHECK_STATUS(status);
130 	}
131 	/* Check if the asym service is enabled */
132 	if (SalCtrl_IsServiceEnabled(enabled_services,
133 				     SAL_SERVICE_TYPE_CRYPTO_ASYM) ||
134 	    SalCtrl_IsServiceEnabled(enabled_services,
135 				     SAL_SERVICE_TYPE_CRYPTO)) {
136 		status = SalStatistics_GetStatEnabled(
137 		    device,
138 		    SAL_STATS_CFG_DH,
139 		    &pStatsCollection->bDhStatsEnabled);
140 		LAC_CHECK_STATUS(status);
141 
142 		status = SalStatistics_GetStatEnabled(
143 		    device,
144 		    SAL_STATS_CFG_DSA,
145 		    &pStatsCollection->bDsaStatsEnabled);
146 		LAC_CHECK_STATUS(status);
147 
148 		status = SalStatistics_GetStatEnabled(
149 		    device,
150 		    SAL_STATS_CFG_ECC,
151 		    &pStatsCollection->bEccStatsEnabled);
152 		LAC_CHECK_STATUS(status);
153 
154 		status = SalStatistics_GetStatEnabled(
155 		    device,
156 		    SAL_STATS_CFG_KEYGEN,
157 		    &pStatsCollection->bKeyGenStatsEnabled);
158 		LAC_CHECK_STATUS(status);
159 
160 		status = SalStatistics_GetStatEnabled(
161 		    device,
162 		    SAL_STATS_CFG_LN,
163 		    &pStatsCollection->bLnStatsEnabled);
164 		LAC_CHECK_STATUS(status);
165 
166 		status = SalStatistics_GetStatEnabled(
167 		    device,
168 		    SAL_STATS_CFG_PRIME,
169 		    &pStatsCollection->bPrimeStatsEnabled);
170 		LAC_CHECK_STATUS(status);
171 
172 		status = SalStatistics_GetStatEnabled(
173 		    device,
174 		    SAL_STATS_CFG_RSA,
175 		    &pStatsCollection->bRsaStatsEnabled);
176 		LAC_CHECK_STATUS(status);
177 	}
178 
179 	/* Check if the sym service is enabled */
180 	if (SalCtrl_IsServiceEnabled(enabled_services,
181 				     SAL_SERVICE_TYPE_CRYPTO_SYM) ||
182 	    SalCtrl_IsServiceEnabled(enabled_services,
183 				     SAL_SERVICE_TYPE_CRYPTO)) {
184 		status = SalStatistics_GetStatEnabled(
185 		    device,
186 		    SAL_STATS_CFG_SYM,
187 		    &pStatsCollection->bSymStatsEnabled);
188 		LAC_CHECK_STATUS(status);
189 	}
190 	return status;
191 };
192 
193 /* @ingroup SalStats */
194 CpaStatus
195 SalStatistics_CleanStatisticsCollection(icp_accel_dev_t *device)
196 {
197 	sal_statistics_collection_t *pStatsCollection = NULL;
198 	LAC_CHECK_NULL_PARAM(device);
199 	pStatsCollection = (sal_statistics_collection_t *)device->pQatStats;
200 	LAC_OS_FREE(pStatsCollection);
201 	device->pQatStats = NULL;
202 	return CPA_STATUS_SUCCESS;
203 }
204