/***************************************************************************
 *
 *   SPDX-License-Identifier: BSD-3-Clause
 *   Copyright(c) 2007-2026 Intel Corporation
 * 
 *   These contents may have been developed with support from one or more
 *   Intel-operated generative artificial intelligence solutions.
 *
 ***************************************************************************/

/**
 ***************************************************************************
 * @file lac_sal_types.h
 *
 * @ingroup SalCtrl
 *
 * Generic instance type definitions of SAL controller
 *
 ***************************************************************************/

#ifndef LAC_SAL_TYPES_H
#define LAC_SAL_TYPES_H

#include "lac_sync.h"
#include "lac_list.h"
#include "icp_accel_devices.h"
#include "sal_statistics.h"

#define SAL_CFG_BASE_DEC 10
#define SAL_CFG_BASE_HEX 16

/**
 *****************************************************************************
 * @ingroup SalCtrl
 *      Instance States
 *
 * @description
 *    An enumeration containing the possible states for an instance.
 *
 *****************************************************************************/
typedef enum sal_service_state_s
{
    SAL_SERVICE_STATE_UNINITIALIZED = 0,
    SAL_SERVICE_STATE_INITIALIZING,
    SAL_SERVICE_STATE_INITIALIZED,
    SAL_SERVICE_STATE_RUNNING,
    SAL_SERVICE_STATE_SHUTTING_DOWN,
    SAL_SERVICE_STATE_SHUTDOWN,
    SAL_SERVICE_STATE_RESTARTING,
    SAL_SERVICE_STATE_ERROR,
    SAL_SERVICE_STATE_END
} sal_service_state_t;

/**
 *****************************************************************************
 * @ingroup SalCtrl
 *      Service Instance Types
 *
 * @description
 *      An enumeration containing the possible types for a service.
 *
 *****************************************************************************/
typedef enum
{
    SAL_SERVICE_TYPE_UNKNOWN = 0,
    /* symmetric and asymmetric crypto service */
    SAL_SERVICE_TYPE_CRYPTO = 1,
    /* legacy compression service (includes both compression and decompression
       directions) */
    SAL_SERVICE_TYPE_COMPRESSION = 2,
    /* inline service */
    SAL_SERVICE_TYPE_INLINE = 4,
    /* asymmetric crypto only service*/
    SAL_SERVICE_TYPE_CRYPTO_ASYM = 8,
    /* symmetric crypto only service*/
    SAL_SERVICE_TYPE_CRYPTO_SYM = 16,
    /* decompression only service */
    SAL_SERVICE_TYPE_DECOMPRESSION = 32
} sal_service_type_t;

/**
 *****************************************************************************
 * @ingroup SalCtrl
 *      Generic Instance Container
 *
 * @description
 *      Contains all the common information across the different instances.
 *
 *****************************************************************************/
typedef struct sal_service_s
{
    sal_service_type_t type;
    /**< Service type (e.g. SAL_SERVICE_TYPE_CRYPTO)*/

    Cpa8U state;
    /**< Status of the service instance
       (e.g. SAL_SERVICE_STATE_INITIALIZED) */

    Cpa32U instance;
    /**< Instance number */

    CpaVirtualToPhysical *virt2PhysClient;
    /**< Pointer to function pointer to client supplied virt_to_phys */

    CpaStatus (*init)(icp_accel_dev_t *device, struct sal_service_s *service);
    /**< Function pointer for instance INIT function */
    CpaStatus (*start)(icp_accel_dev_t *device, struct sal_service_s *service);
    /**< Function pointer for instance START function */
    CpaStatus (*stop)(icp_accel_dev_t *device, struct sal_service_s *service);
    /**< Function pointer for instance STOP function */
    CpaStatus (*shutdown)(icp_accel_dev_t *device,
                          struct sal_service_s *service);
    /**< Function pointer for instance SHUTDOWN function */
    CpaStatus (*error)(icp_accel_dev_t *device, struct sal_service_s *service);
    /**< Function pointer for instance ERROR function */

    CpaStatus (*restarting)(icp_accel_dev_t *device,
                            struct sal_service_s *service);
    /**< Function pointer for instance RESTARTING function */

    CpaStatus (*restarted)(icp_accel_dev_t *device,
                           struct sal_service_s *service);
    /**< Function pointer for instance RESTARTED function */

    CpaCyInstanceNotificationCbFunc notification_cb;
    /**< Function pointer for instance restarting handler */

    void *cb_tag;
    /**< Restarting handler priv data */

    sal_statistics_collection_t *stats;
    /**< Pointer to device statistics configuration */

    Cpa32U capabilitiesMask;
    /**< Capabilities mask of the device */

    Cpa32U dcExtendedFeatures;
    /**< Bit field of features. I.e. Compress And Verify */

    CpaBoolean isInstanceStarted;
    /**< True if user called StartInstance on this instance */

    CpaBoolean isGen4;
    /* True if the device is a Gen4 device (qat_4xxx/qat_4xxxvf or
     * qat_420xx/qat_420xxvf) */

    CpaBoolean isGen6;
    /* True if the device is qat_6xxxvf or qat_6xxx */
    CpaBoolean dpaSupport;
    /* True if the device has DPA support */
    CpaBoolean optimisedCurveSupport;
    /* True if optimised curves are supported */

    CpaBoolean ns_isCnvErrorInjection;
    /** < Cnv Error Injection simulation is enabled.
     * This var is needed in the instance as there is no session
     * structure available to store it for the NS case. */

} sal_service_t;

/**
 *****************************************************************************
 * @ingroup SalCtrl
 *      SAL structure
 *
 * @description
 *      Contains lists to crypto and compression instances.
 *
 *****************************************************************************/
typedef struct sal_s
{
    sal_list_t *crypto_services;
    /**< Container of sal_crypto_service_t */
    sal_list_t *asym_services;
    /**< Container of sal_asym_service_t */
    sal_list_t *sym_services;
    /**< Container of sal_sym_service_t */
    sal_list_t *compression_services;
    /**< Container of sal_compression_service_t */
    sal_list_t *decompression_services;
    /**< Container of sal_compression_service_t for decomp only service */
} sal_t;

/**
 *******************************************************************************
 * @ingroup SalCtrl
 *      This macro verifies that the right service type has been passed in.
 *
 * @param[in] pService         pointer to service instance
 * @param[in] service_type     service type to check againstx.
 *
 * @return CPA_STATUS_FAIL     Parameter is incorrect type
 *
 ******************************************************************************/
#define SAL_CHECK_INSTANCE_TYPE(pService, service_type)                        \
    do                                                                         \
    {                                                                          \
        sal_service_t *pGenericService = NULL;                                 \
        pGenericService = (sal_service_t *)pService;                           \
        if (!(service_type & pGenericService->type))                           \
        {                                                                      \
            LAC_LOG_ERROR("The instance handle is the wrong type");            \
            return CPA_STATUS_FAIL;                                            \
        }                                                                      \
    } while (0)

#endif
