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

📄 ssx31bpke.c

📁 海思KEY驱动
💻 C
📖 第 1 页 / 共 4 页
字号:



	SplX(lIntMask);
    return OK;


error:
	SplX(lIntMask); 
    
    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);
	    }

   
    return ERR;
}

/*********************************************************DH*******************************************************************************/
/*********************************************************DH*******************************************************************************/
/*********************************************************DH*******************************************************************************/
ULONG SSX31B_SetBDSrcDhPub(SSX31B_PKE_BD * pCurBD, SSX31BDH_S* pSd707Dh)
{
	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= pSd707Dh->ulNlen + pSd707Dh->ulXalen + pSd707Dh->ulGlen;
    pCurBD->ulGatherEnable = CD_GATHER_ENABLE;
    
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pSd707Dh->pcN);
    pGatherDesc->ulInputLen = pSd707Dh->ulNlen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;
    
    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pSd707Dh->pcXa);
    pGatherDesc->ulInputLen = pSd707Dh->ulXalen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = pSd707Dh->ucXaFlg;
    
    pGatherDesc ++;        
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pSd707Dh->pcG);
    pGatherDesc->ulInputLen = pSd707Dh->ulGlen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

    return OK;
}
ULONG SSX31B_SetBDDstDhPub(SSX31B_PKE_BD * pCurBD, UCHAR* pcYa, ULONG ulYalen, UCHAR* pcXaOut, ULONG ulXaOutlen )
{
	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->ulOutputLength1= ulXaOutlen;
    pCurBD->ulOutputLength0= ulYalen;
    pCurBD->ulScatterEnable = CD_SCATTER_ENABLE;
  
    pScatterDesc->ulOutputPtr = (ULONG)virt_to_bus(pcYa);
    pScatterDesc->ulOutputLen = ulYalen;
    pScatterDesc->ulOutputAttrib = OCD_LAST_SLICE_PKE;
    pScatterDesc->ulOrig = 0;
    
        
    pScatterDesc ++;
    pScatterDesc->ulOutputPtr = (ULONG)virt_to_bus(pcXaOut);
    pScatterDesc->ulOutputLen = ulXaOutlen;
    pScatterDesc->ulOutputAttrib = OCD_LAST_SLICE_PKE;
    pScatterDesc->ulOrig = 0;
    
    
    return OK;
}

ULONG SSX31B_SetBDSrcDhPri(SSX31B_PKE_BD * pCurBD, SSX31BDH_S* pSd707Dh)
{
	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= pSd707Dh->ulNlen + pSd707Dh->ulXalen + pSd707Dh->ulYblen;
    pCurBD->ulGatherEnable = CD_GATHER_ENABLE;
    
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pSd707Dh->pcN);
    pGatherDesc->ulInputLen = pSd707Dh->ulNlen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;
    
    pGatherDesc ++;
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pSd707Dh->pcXa);
    pGatherDesc->ulInputLen = pSd707Dh->ulXalen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;
    
    pGatherDesc ++;    
    pGatherDesc->ulInputPtr = (ULONG)virt_to_bus(pSd707Dh->pcYb);
    pGatherDesc->ulInputLen = pSd707Dh->ulYblen;
    pGatherDesc->ulInputAttrib = ICD_LAST_SLICE_PKE;
    pGatherDesc->ulOrig = 0;

    return OK;
}

ULONG SSX31B_SetBDDstDhPri(SSX31B_PKE_BD * pCurBD, CHAR* pcXab, ULONG ulXablen)
{
    pCurBD->ulOutputContext = (ULONG)virt_to_bus(pcXab);
    pCurBD->ulOutputLength0 = ulXablen;
    pCurBD->ulOutputNum  = 0;
    pCurBD->ulScatterEnable &= ~CD_SCATTER_ENABLE;
    
    return OK;
}


ULONG SSX31B_DH(SSX31BDRV_CTRL* pDrvCtrl, SSX31BDH_S* pSd707Dh)
{
    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;	
    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 ( pSd707Dh->ucOpCode )
    {
	case SSX31B_OPER_DH_PUBKEY:
	{     
		
		pRes = Malloc( pSd707Dh->ulYalen + 4);
		if (pRes == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned = four_bytes_align (pRes); 
		
		pRes1 = Malloc( pSd707Dh->ulXaOutlen + 4);
		if (pRes1 == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned1 = four_bytes_align (pRes1); 
		
		if ( SSX31B_SetBDSrcDhPub( pCurBD, pSd707Dh ) == ERR )
		{
			goto error;						
		}

		if ( SSX31B_SetBDDstDhPub(pCurBD, pResAligned, pSd707Dh->ulYalen, pResAligned1, pSd707Dh->ulXaOutlen) == ERR )
	    	{
	    		goto error;
		}
		
		break;
	}	
	
	case SSX31B_OPER_DH_PRIVKEY:   
	{ 
		pRes = Malloc( pSd707Dh->ulXablen + 4);
		if (pRes == NULL)
		{
			PRINT ("\n can't malloc!");
			goto error;
		}
		pResAligned = four_bytes_align (pRes); 
		
		if ( SSX31B_SetBDSrcDhPri(pCurBD, pSd707Dh ) == ERR )
		{
			goto error;
		}

		if ( SSX31B_SetBDDstDhPri(pCurBD, pResAligned, pSd707Dh->ulXablen) == ERR )
	   	{
	    		goto error;
		}

		break;
	}

	default:
		break;
	}
    

    pCurBD->ulOpCode = pSd707Dh->ucOpCode;
    pCurBD->ulFlag = pSd707Dh->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<3; 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;
	}

	pulTmp = (ULONG*)pCurBD;
	pulTmp1 = (ULONG*)((ULONG)bus_to_virt ( pulTmp[3] ));
	for (i=0; i<2; i++)
	{
		PRINT ("\r\nocd %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 0
{
	PRINT ("\r\nres: %08x %08x %08x", *((ULONG*)pResAligned),  *((ULONG*)pResAligned+4), *((ULONG*)pResAligned+8) );
	PRINT ("\r\nres1: %08x %08x %08x", *((ULONG*)pResAligned1),  *((ULONG*)pResAligned1+4), *((ULONG*)pResAligned1+8) );
}
#endif	    
	    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;
               }
           }
	   
   	   switch ( pSd707Dh->ucOpCode )
    	   {
		case SSX31B_OPER_DH_PUBKEY:
		{     
		
	            if (pResAligned != NULL)
	            {
		          memcpy (pSd707Dh->pcYa, pResAligned, pSd707Dh->ulYalen);
    	            }
	            if (pResAligned1 != NULL)
	            {
		          memcpy (pSd707Dh->pcXaOut, pResAligned1, pSd707Dh->ulXaOutlen);
              	    }
  	
			break;
		}	
	
		case SSX31B_OPER_DH_PRIVKEY:   
		{ 
	            if (pResAligned != NULL)
	            {
		          memcpy (pSd707Dh->pcXab, pResAligned, pSd707Dh->ulXablen);
    	            }

			break;
		}

		default:
			break;
	    }
	    
	    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;


    }

	SplX(lIntMask);
    return OK;


error:
	SplX(lIntMask); 
    
    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);
	    }
   
    return ERR;
}

⌨️ 快捷键说明

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