⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssx31bpke.c

📁 海思KEY驱动
💻 C
📖 第 1 页 / 共 4 页
字号:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/errno.h>

#include <linux/slab.h>
#include <linux/delay.h>


#include "../def.h"
#include "SSX31BDrv.h"
#include "SSX31BPke.h"
#include "../SSX31.h"

/**************/
extern ULONG  four_bytes_align (ULONG ulVirAddr);
/**************/

/* use basic bd and basic rd */
/*****************************************************MOD****************************************************************************/
/*****************************************************MOD****************************************************************************/
/*****************************************************MOD****************************************************************************/

ULONG SSX31B_SetBDDstModDouble(SSX31B_PKE_BD * pCurBD, UCHAR* pcRes, ULONG ulResLen, UCHAR* pcRes1, ULONG ulRes1Len)
{
	SSX31B_PKE_OCD* pScatterDesc = NULL;
	ULONG* ulTmp;

    pScatterDesc = (SSX31B_PKE_OCD*)Malloc( 2 * sizeof(SSX31B_PKE_OCD) );

	
    if (pScatterDesc == NULL)
    {
        PRINT("SSX31B driver PE send mem alloc fail\r\n");
        return ERR;                               
    }

    pCurBD->ulOutputContext  = (ULONG)virt_to_bus(pScatterDesc);  
    pCurBD->ulOutputNum = 2;
    pCurBD->ulOutputLength0= ulResLen + ulRes1Len;
    pCurBD->ulScatterEnable = CD_SCATTER_ENABLE;
  
    pScatterDesc->ulOutputPtr = (ULONG)virt_to_bus(pcRes);
    pScatterDesc->ulOutputLen = ulResLen;
    pScatterDesc->ulOutputAttrib = OCD_LAST_SLICE_PKE;
    pScatterDesc->ulOrig = 0;
        
    pScatterDesc ++;
    pScatterDesc->ulOutputPtr = (ULONG)virt_to_bus(pcRes1);
    pScatterDesc->ulOutputLen = ulRes1Len;
    pScatterDesc->ulOutputAttrib = OCD_LAST_SLICE_PKE;
    pScatterDesc->ulOrig = 0;
	
	return OK;
}

ULONG SSX31B_SetBDSrcModDouble(SSX31B_PKE_BD * pCurBD, UCHAR* pcModulusN, ULONG ulModulusNLen, UCHAR* pcA, ULONG ulALen, UCHAR* pcB, ULONG
ulBLen, UCHAR* pcModulusN1, ULONG ulModulusN1Len, UCHAR* pcA1, ULONG ulA1Len, UCHAR* pcB1, ULONG ulB1Len)
{
	SSX31B_PKE_ICD* pGatherDesc = NULL;
	int i;
	ULONG* ulTmp;
	
    pGatherDesc = (SSX31B_PKE_ICD*)Malloc( 6 * sizeof(SSX31B_PKE_ICD) );

	
    if (pGatherDesc == NULL)
    {
        PRINT("SSX31B driver PE send mem alloc fail\r\n");
        return ERR;                               
    }

    pCurBD->ulInputContext  = (ULONG)virt_to_bus(pGatherDesc);  
    pCurBD->ulInputNum = 6;
    pCurBD->ulInputLength= ulModulusNLen + ulALen + ulBLen + ulModulusN1Len + ulA1Len + ulB1Len;
    pCurBD->ulGatherEnable = CD_GATHER_ENABLE;

  
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcModulusN);
    pGatherDesc->ulInputLen = ulModulusNLen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcModulusN1);
    pGatherDesc->ulInputLen = ulModulusN1Len;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

        
    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcB);
    pGatherDesc->ulInputLen = ulBLen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

    
    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcB1);
    pGatherDesc->ulInputLen = ulB1Len;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

        
        
    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcA);
    pGatherDesc->ulInputLen = ulALen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

        
        
    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcA1);
    pGatherDesc->ulInputLen = ulA1Len;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;


	

	
	return OK;	
}

ULONG SSX31B_SetBDSrcModGet(SSX31B_PKE_BD * pCurBD, UCHAR* pcModulusN, ULONG ulModulusNLen, UCHAR* pcA, ULONG ulALen)
{
	SSX31B_PKE_ICD* pGatherDesc = NULL;

    pGatherDesc = (SSX31B_PKE_ICD*)Malloc( 2 * sizeof(SSX31B_PKE_ICD) );

	
    if (pGatherDesc == NULL)
    {
        PRINT("SSX31B driver PE send mem alloc fail\r\n");
        return ERR;                               
    }

    pCurBD->ulInputContext  = (ULONG)virt_to_bus(pGatherDesc);  
    pCurBD->ulInputNum = 2;
    pCurBD->ulInputLength= ulModulusNLen + ulALen;
    pCurBD->ulGatherEnable = CD_GATHER_ENABLE;
    
	pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcModulusN);
	pGatherDesc->ulInputLen = ulModulusNLen;
	pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
	pGatherDesc->ulOrig = 0;
	
	pGatherDesc ++;
	pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcA);
	pGatherDesc->ulInputLen = ulALen;
	pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
	pGatherDesc->ulOrig = 0;

	return OK;
}

ULONG SSX31B_SetBDDstModNormal(SSX31B_PKE_BD * pCurBD, UCHAR* pcRes, ULONG ulResLen)
{
    pCurBD->ulOutputContext = (ULONG)virt_to_bus(pcRes);
    pCurBD->ulOutputLength0 = ulResLen;
    pCurBD->ulOutputNum  = 0;
    pCurBD->ulScatterEnable &= ~CD_SCATTER_ENABLE;
    
    return OK;
}

ULONG SSX31B_SetBDSrcModNormal(SSX31B_PKE_BD * pCurBD, UCHAR* pcModulusN, ULONG ulModulusNLen, UCHAR* pcA, ULONG ulALen, UCHAR* pcB, ULONG ulBLen)
{
	SSX31B_PKE_ICD* pGatherDesc = NULL;

    pGatherDesc = (SSX31B_PKE_ICD*)Malloc( 3 * sizeof(SSX31B_PKE_ICD) );

	
    if (pGatherDesc == NULL)
    {
        PRINT("SSX31B driver PE send mem alloc fail\r\n");
        return ERR;                               
    }

    pCurBD->ulInputContext  = (ULONG)virt_to_bus(pGatherDesc);  
    pCurBD->ulInputNum = 3;
    pCurBD->ulInputLength= ulModulusNLen + ulALen + ulBLen;
    pCurBD->ulGatherEnable = CD_GATHER_ENABLE;
    
	pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcModulusN);
	pGatherDesc->ulInputLen = ulModulusNLen;
	pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
	pGatherDesc->ulOrig = 0;
	

	pGatherDesc ++;
	pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcA);
	pGatherDesc->ulInputLen = ulALen;
	pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
	pGatherDesc->ulOrig = 0;


	pGatherDesc ++;
	pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pcB);
	pGatherDesc->ulInputLen = ulBLen;
	pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
	pGatherDesc->ulOrig = 0;



	return OK;
}


ULONG SSX31B_MOD(SSX31BDRV_CTRL* pDrvCtrl, SSX31BMOD_S* pSd707Mod)
{
    SSX31B_PKE_BD * pCurBD = NULL;
    SSX31B_PKE_RD * pCurRD = NULL;
    UCHAR* pRes = NULL;
    UCHAR* pRes1 = NULL;
    UCHAR* pResAligned = NULL;  
    UCHAR* pResAligned1 = NULL;  
    ULONG ulTmt = 0;          
    int i;
ULONG* ulTmp;
    LONG lIntMask;


    if ( pDrvCtrl->ulCurrPkeBdReadPtr == ((pDrvCtrl->ulCurrPkeBdWritePtr + 1) & (SSX31B_PKE_BDQUEUE_LEN - 1))) 
    {
        PRINT("BD Queue full:BD Read:%d write %d", 
            pDrvCtrl->ulCurrPkeBdReadPtr,
            pDrvCtrl->ulCurrPkeBdWritePtr);
        return ERR;
    }

    SplImp(&lIntMask);


    pCurBD = &((SSX31B_PKE_BD *)(pDrvCtrl->ulPkeBDMemBase))[pDrvCtrl->ulCurrPkeBdWritePtr];

    switch ( pSd707Mod->ucOpCode )
    {
	case SSX31B_OPER_DH_MODULAR_ADD:
	case SSX31B_OPER_DH_MODULAR_SUB:
	case SSX31B_OPER_DH_MODULAR_MULTIPLY:
	case SSX31B_OPER_DH_MODULAR_PWR:
	{     
		
		pRes = Malloc( pSd707Mod->ulResLen + 4);
		if (pRes == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned = four_bytes_align (pRes); 
		
		if ( SSX31B_SetBDSrcModNormal( pCurBD, pSd707Mod->pcModulusN, pSd707Mod->ulModulusNLen, pSd707Mod->pcA, pSd707Mod->ulALen,
		                                                               pSd707Mod->pcB, pSd707Mod->ulBLen ) == ERR )
		{
			goto error;						
		}

		if ( SSX31B_SetBDDstModNormal(pCurBD, pResAligned, pSd707Mod->ulResLen) == ERR )
	    	{
	    		goto error;
		}
		
		break;
	}	
	
	case SSX31B_OPER_DH_MODULAR:   
	{ 
		pRes = Malloc( pSd707Mod->ulResLen + 4);
		if (pRes == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned = four_bytes_align (pRes); 
		
		if ( SSX31B_SetBDSrcModGet(pCurBD, pSd707Mod->pcModulusN, pSd707Mod->ulModulusNLen, pSd707Mod->pcA, pSd707Mod->ulALen ) == ERR )
		{
			goto error;
		}

		if ( SSX31B_SetBDDstModNormal(pCurBD, pResAligned, pSd707Mod->ulResLen) == ERR )
	   	{
	    		goto error;
		}

		break;
	}

	case SSX31B_OPER_DH_DUAL_MODULAR_PWR: 
	{
		pRes = Malloc( pSd707Mod->ulResLen + 4);
		if (pRes == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned = four_bytes_align (pRes); 
		
		pRes1 = Malloc( pSd707Mod->ulRes1Len + 4);
		if (pRes1 == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned1 = four_bytes_align (pRes1); 
	
		if ( SSX31B_SetBDSrcModDouble(pCurBD, pSd707Mod->pcModulusN, pSd707Mod->ulModulusNLen, pSd707Mod->pcA, pSd707Mod->ulALen,
		pSd707Mod->pcB, pSd707Mod->ulBLen, pSd707Mod->pcModulusN1, pSd707Mod->ulModulusN1Len, pSd707Mod->pcA1, pSd707Mod->ulA1Len,
		pSd707Mod->pcB1, pSd707Mod->ulB1Len ) == ERR )
		{
			goto error;
		}
		if ( SSX31B_SetBDDstModDouble(pCurBD, pResAligned, pSd707Mod->ulResLen, pResAligned1, pSd707Mod->ulRes1Len) == ERR )
		{
			goto error;
		}
		
		break;
		
 	}
	default:
		break;
	}
    

    pCurBD->ulOpCode = pSd707Mod->ucOpCode;
    pCurBD->ulFlag = pSd707Mod->ucFlg;

#if 0
{
	ULONG* pulTmp = NULL;
	ULONG* pulTmp1 = NULL;
	int i;
	
	pulTmp = (ULONG*)pCurBD;
	
	PRINT ("\r\nBD: %08x %08x %08x %08x %08x %08x ", pulTmp[0], pulTmp[1], pulTmp[2], pulTmp[3], pulTmp[4], pulTmp[5]); 
	
	pulTmp1 = (ULONG*)((ULONG)bus_to_virt ( pulTmp[1] ));
	for (i=0; i<6; i++)
	{
		PRINT ("\r\nicd %d: %08x %08x", i, pulTmp1[0], pulTmp1[1]);
		pulTmp = (ULONG*)((ULONG)bus_to_virt ( pulTmp1[0] ));
		PRINT ("\r\n buf: %08x", pulTmp[0]);
		
		pulTmp1 += 2;
	}
	
}
#endif
    



    pDrvCtrl->ulCurrPkeBdWritePtr = (pDrvCtrl->ulCurrPkeBdWritePtr + 1) & (SSX31B_PKE_BDQUEUE_LEN - 1); 
    PciWrite32(SSX31B_REG_BQWP2, pDrvCtrl->ulCurrPkeBdWritePtr);
    

    pDrvCtrl->ulFreePkeBDs --;    

   
   

    ulTmt = (DMA2_TIMEOUT_DMA2)*100 + 100000;
    
    for (i = 0; i < ulTmt; i ++)
    {
        UsDelay(10);


        PciRead32(SSX31B_REG_BQRP2, &pDrvCtrl->ulCurrPkeBdReadPtr);
        

        PciRead32(SSX31B_REG_RQWP2, &pDrvCtrl->ulCurrPkeRdWritePtr);


	
        if (pDrvCtrl->ulCurrPkeRdWritePtr == pDrvCtrl->ulCurrPkeRdReadPtr)
        {
            continue;
        }
        else
        {

            pCurRD = &((SSX31B_PKE_RD *)(pDrvCtrl->ulPkeRDMemBase))[pDrvCtrl->ulCurrPkeRdReadPtr];
	
	    if ((pCurRD->ulStatus & DMA1_ERR_MASK) != MDA1_STATUS_OK)
	    {
	    	PRINT ("\nPKE err!");
		PRINT ("\nstatus: %04x", pCurRD->ulStatus);
		goto error; 
	    }
  
	    
	    if (pCurRD->ulGatherEnable == CD_GATHER_ENABLE )
    	    {
    	    	if (pCurRD->ulInputContext )
            	{
            		
           		 Free( bus_to_virt(pCurRD->ulInputContext) );
        	         pCurRD->ulInputContext = NULL;
            	}
            }
           if (pCurRD->ulScatterEnable == CD_SCATTER_ENABLE )
           {
               if (pCurRD->ulOutputContext )
               {

                   Free( bus_to_virt(pCurRD->ulOutputContext) );
                   pCurRD->ulOutputContext = NULL;
               }
           }
	   

   
	    if (pResAligned != NULL)
	    {
		    memcpy (pSd707Mod->pcRes, pResAligned, pSd707Mod->ulResLen);
    	    }
	    if (pResAligned1 != NULL)
	    {
		    memcpy (pSd707Mod->pcRes1, pResAligned1, pSd707Mod->ulRes1Len);
    	    }

	    if (pRes != NULL)
	    {
	    	Free (pRes);
	    }
	    if (pRes1 != NULL)
	    {
	    	Free (pRes1);
	    }
	    
            pDrvCtrl->ulCurrPkeRdReadPtr = (pDrvCtrl->ulCurrPkeRdReadPtr + 1) & (SSX31B_PKE_RDQUEUE_LEN - 1);  



            if (pDrvCtrl->ulCurrPkeRdWritePtr == pDrvCtrl->ulCurrPkeRdReadPtr)
            {

                PciWrite32(SSX31B_REG_RQRP2, pDrvCtrl->ulCurrPkeRdReadPtr); 

               pDrvCtrl->ulFreePkeBDs ++;
               pDrvCtrl->ulTmtPKEDone ++;

               break;
            }

        }
	

    }

    if (i >= ulTmt)
    {
#ifdef HI_DBG    
    	PRINT ("\r\nnot finish");
#endif	

	    if (pCurBD->ulGatherEnable == CD_GATHER_ENABLE )
    	    {
    	    	if (pCurBD->ulInputContext )
            	{
            		
           		 Free( bus_to_virt(pCurBD->ulInputContext) );
        	         pCurBD->ulInputContext = NULL;
            	}
            }
           if (pCurBD->ulScatterEnable == CD_SCATTER_ENABLE )
           {
               if (pCurBD->ulOutputContext )
               {

                   Free( bus_to_virt(pCurBD->ulOutputContext) );
                   pCurBD->ulOutputContext = NULL;
               }
           }
	   

	    if (pRes != NULL)
	    {
	    	Free (pRes);
	    }
	    if (pRes1 != NULL)
	    {
	    	Free (pRes1);
	    }

        SplX(lIntMask);
    
    
        return ERR;


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -