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