aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic/files/crypto/ocf/kirkwood/cesa/mvCesa.h
blob: c0abc9b7ead243f147916d1dfce5262e8e8def3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*******************************************************************************
Copyright (C) Marvell International Ltd. and its affiliates

This software file (the "File") is owned and distributed by Marvell
International Ltd. and/or its affiliates ("Marvell") under the following
alternative licensing terms.  Once you have made an election to distribute the
File under one of the following license alternatives, please (i) delete this
introductory statement regarding license alternatives, (ii) delete the two
license alternatives that you have not elected to use and (iii) preserve the
Marvell copyright notice above.

********************************************************************************
Marvell Commercial License Option

If you received this File from Marvell and you have entered into a commercial
license agreement (a "Commercial License") with Marvell, the File is licensed
to you under the terms of the applicable Commercial License.

********************************************************************************
Marvell GPL License Option

If you received this File from Marvell, you may opt to use, redistribute and/or
modify this File in accordance with the terms and conditions of the General
Public License Version 2, June 1991 (the "GPL License"), a copy of which is
available along with the File in the license.txt file or by writing to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
on the worldwide web at http://www.gnu.org/licenses/gpl.txt.

THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
DISCLAIMED.  The GPL License provides additional details about this warranty
disclaimer.
********************************************************************************
Marvell BSD License Option

If you received this File from Marvell, you may opt to use, redistribute and/or
modify this File under the following licensing terms.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    *   Redistributions of source code must retain the above copyright notice,
	    this list of conditions and the following disclaimer.

    *   Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.

    *   Neither the name of Marvell nor the names of its contributors may be
        used to endorse or promote products derived from this software without
        specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
* mvCesa.h - Header File for Cryptographic Engines and Security Accelerator
*
* DESCRIPTION:
*       This header file contains macros typedefs and function declaration for
*       the Marvell Cryptographic Engines and Security Accelerator.
*
*******************************************************************************/

#ifndef __mvCesa_h__
#define __mvCesa_h__

#include "mvOs.h"
#include "mvCommon.h"
#include "mvDebug.h"

#include "ctrlEnv/mvCtrlEnvSpec.h"

#include "cesa/mvMD5.h"
#include "cesa/mvSHA1.h"

#include "cesa/mvCesa.h"
#include "cesa/AES/mvAes.h"
#include "mvSysHwConfig.h"

#ifdef MV_INCLUDE_IDMA
#include "idma/mvIdma.h"
#include "idma/mvIdmaRegs.h"
#else
/* Redefine MV_DMA_DESC structure */
typedef struct _mvDmaDesc
{
    MV_U32 	byteCnt;        /* The total number of bytes to transfer        */
    MV_U32 	phySrcAdd;	    /* The physical source address                  */
    MV_U32 	phyDestAdd;     /* The physical destination address             */
    MV_U32	phyNextDescPtr; /* If we are using chain mode DMA transfer,     */
				            /* then this pointer should point to the        */
                            /* physical address of the next descriptor,     */
                            /* otherwise it should be NULL.                 */
}MV_DMA_DESC;
#endif /* MV_INCLUDE_IDMA */

#include "cesa/mvCesaRegs.h"

#define MV_CESA_AUTH_BLOCK_SIZE         64 /* bytes */

#define MV_CESA_MD5_DIGEST_SIZE         16 /* bytes */
#define MV_CESA_SHA1_DIGEST_SIZE        20 /* bytes */

#define MV_CESA_MAX_DIGEST_SIZE         MV_CESA_SHA1_DIGEST_SIZE

#define MV_CESA_DES_KEY_LENGTH          8   /* bytes = 64 bits */
#define MV_CESA_3DES_KEY_LENGTH         24  /* bytes = 192 bits */
#define MV_CESA_AES_128_KEY_LENGTH      16  /* bytes = 128 bits */
#define MV_CESA_AES_192_KEY_LENGTH      24  /* bytes = 192 bits */
#define MV_CESA_AES_256_KEY_LENGTH      32  /* bytes = 256 bits */

#define MV_CESA_MAX_CRYPTO_KEY_LENGTH   MV_CESA_AES_256_KEY_LENGTH

#define MV_CESA_DES_BLOCK_SIZE          8 /* bytes = 64 bits */
#define MV_CESA_3DES_BLOCK_SIZE         8 /* bytes = 64 bits */

#define MV_CESA_AES_BLOCK_SIZE          16 /* bytes = 128 bits */

#define MV_CESA_MAX_IV_LENGTH           MV_CESA_AES_BLOCK_SIZE

#define MV_CESA_MAX_MAC_KEY_LENGTH      64 /* bytes */

typedef struct
{
	MV_U8               cryptoKey[MV_CESA_MAX_CRYPTO_KEY_LENGTH];
	MV_U8               macKey[MV_CESA_MAX_MAC_KEY_LENGTH];
	MV_CESA_OPERATION   operation;
	MV_CESA_DIRECTION   direction;
	MV_CESA_CRYPTO_ALG  cryptoAlgorithm;
	MV_CESA_CRYPTO_MODE cryptoMode;
   	MV_U8               cryptoKeyLength;
	MV_CESA_MAC_MODE    macMode;
	MV_U8               macKeyLength;
	MV_U8               digestSize;

} MV_CESA_OPEN_SESSION;

typedef struct
{
    MV_BUF_INFO *pFrags;
	MV_U16	    numFrags;
    MV_U16      mbufSize;

} MV_CESA_MBUF;

typedef struct
{
    void* 	pReqPrv; /* instead of reqId */
    MV_U32 	retCode;
    MV_16   sessionId;

} MV_CESA_RESULT;

typedef void    (*MV_CESA_CALLBACK) (MV_CESA_RESULT* pResult);


typedef struct
{
    void*               pReqPrv;    /* instead of reqId */
    MV_CESA_MBUF*       pSrc;
    MV_CESA_MBUF*       pDst;
    MV_CESA_CALLBACK*   pFuncCB;
    MV_16               sessionId;
    MV_U16              ivFromUser;
    MV_U16              ivOffset;
    MV_U16              cryptoOffset;
    MV_U16              cryptoLength;
    MV_U16              digestOffset;
    MV_U16              macOffset;
    MV_U16              macLength;
    MV_BOOL		skipFlush;
} MV_CESA_COMMAND;



MV_STATUS   mvCesaHalInit (int numOfSession, int queueDepth, char* pSramBase, MV_U32 cryptEngBase, void *osHandle);
MV_STATUS   mvCesaFinish (void);
MV_STATUS   mvCesaSessionOpen(MV_CESA_OPEN_SESSION *pSession, short* pSid);
MV_STATUS   mvCesaSessionClose(short sid);
MV_STATUS   mvCesaCryptoIvSet(MV_U8* pIV, int ivSize);

MV_STATUS   mvCesaAction (MV_CESA_COMMAND* pCmd);

MV_U32      mvCesaInProcessGet(void);
MV_STATUS   mvCesaReadyDispatch(void);
MV_STATUS   mvCesaReadyGet(MV_CESA_RESULT* pResult);
MV_BOOL     mvCesaIsReady(void);

int     	mvCesaMbufOffset(MV_CESA_MBUF* pMbuf, int offset, int* pBufOffset);
MV_STATUS   mvCesaCopyFromMbuf(MV_U8* pDst, MV_CESA_MBUF* pSrcMbuf,
                               int offset, int size);
MV_STATUS   mvCesaCopyToMbuf(MV_U8* pSrc, MV_CESA_MBUF* pDstMbuf,
                               int offset, int size);
MV_STATUS   mvCesaMbufCopy(MV_CESA_MBUF* pMbufDst, int dstMbufOffset,
                           MV_CESA_MBUF* pMbufSrc, int srcMbufOffset, int size);

/********** Debug functions ********/

void        mvCesaDebugMbuf(const char* str, MV_CESA_MBUF *pMbuf, int offset, int size);
void        mvCesaDebugSA(short sid, int mode);
void        mvCesaDebugStats(void);
void        mvCesaDebugStatsClear(void);
void        mvCesaDebugRegs(void);
void        mvCesaDebugStatus(void);
void        mvCesaDebugQueue(int mode);
void        mvCesaDebugSram(int mode);
void        mvCesaDebugSAD(int mode);


/********  CESA Private definitions ********/
#if (MV_CESA_VERSION >= 2)
#if (MV_CACHE_COHERENCY  == MV_CACHE_COHER_SW)
#define MV_CESA_TDMA_CTRL_VALUE       MV_CESA_TDMA_DST_BURST_MASK(MV_CESA_TDMA_BURST_128B) \
                                    | MV_CESA_TDMA_SRC_BURST_MASK(MV_CESA_TDMA_BURST_128B) \
                                    | MV_CESA_TDMA_OUTSTAND_READ_EN_MASK                   \
				    | MV_CESA_TDMA_NO_BYTE_SWAP_MASK			   \
                                    | MV_CESA_TDMA_ENABLE_MASK
#else
#define MV_CESA_TDMA_CTRL_VALUE       MV_CESA_TDMA_DST_BURST_MASK(MV_CESA_TDMA_BURST_32B)  \
                                    | MV_CESA_TDMA_SRC_BURST_MASK(MV_CESA_TDMA_BURST_128B) \
                                    /*| MV_CESA_TDMA_OUTSTAND_READ_EN_MASK                   */\
                                    | MV_CESA_TDMA_ENABLE_MASK

#endif
#else
#define MV_CESA_IDMA_CTRL_LOW_VALUE   ICCLR_DST_BURST_LIM_128BYTE   \
                                    | ICCLR_SRC_BURST_LIM_128BYTE   \
                                    | ICCLR_INT_MODE_MASK           \
                                    | ICCLR_BLOCK_MODE              \
                                    | ICCLR_CHAN_ENABLE             \
                                    | ICCLR_DESC_MODE_16M
#endif /* MV_CESA_VERSION >= 2 */

#define MV_CESA_MAX_PKT_SIZE        (64 * 1024)
#define MV_CESA_MAX_MBUF_FRAGS      20

#define MV_CESA_MAX_REQ_FRAGS       ( (MV_CESA_MAX_PKT_SIZE / MV_CESA_MAX_BUF_SIZE) + 1)

#define MV_CESA_MAX_DMA_DESC    (MV_CESA_MAX_MBUF_FRAGS*2 + 5)

#define MAX_CESA_CHAIN_LENGTH	20

typedef enum
{
    MV_CESA_IDLE     = 0,
    MV_CESA_PENDING,
    MV_CESA_PROCESS,
    MV_CESA_READY,
#if (MV_CESA_VERSION >= 3)
    MV_CESA_CHAIN,
#endif
} MV_CESA_STATE;


/* Session database */

/* Map of Key materials of the session in SRAM.
 * Each field must be 8 byte aligned
 * Total size: 32 + 24 + 24 = 80 bytes
 */
typedef struct
{
    MV_U8  cryptoKey[MV_CESA_MAX_CRYPTO_KEY_LENGTH];
    MV_U8  macInnerIV[MV_CESA_MAX_DIGEST_SIZE];
    MV_U8  reservedInner[4];
    MV_U8  macOuterIV[MV_CESA_MAX_DIGEST_SIZE];
    MV_U8  reservedOuter[4];

} MV_CESA_SRAM_SA;

typedef struct
{
    MV_CESA_SRAM_SA*    pSramSA;
    MV_U32              config;
    MV_U8               cryptoKeyLength;
    MV_U8               cryptoIvSize;
    MV_U8               cryptoBlockSize;
    MV_U8               digestSize;
    MV_U8               macKeyLength;
    MV_U8               valid;
    MV_U8               ctrMode;
    MV_U32              count;

} MV_CESA_SA;

/* DMA list management */
typedef struct
{
    MV_DMA_DESC*    pDmaFirst;
    MV_DMA_DESC*    pDmaLast;

} MV_CESA_DMA;


typedef struct
{
    MV_U8               numFrag;
    MV_U8               nextFrag;
    int                 bufOffset;
    int                 cryptoSize;
    int                 macSize;
    int                 newDigestOffset;
    MV_U8               orgDigest[MV_CESA_MAX_DIGEST_SIZE];

} MV_CESA_FRAGS;

/* Request queue */
typedef struct
{
    MV_U8               state;
    MV_U8               fragMode;
    MV_U8               fixOffset;
    MV_CESA_COMMAND*    pCmd;
    MV_CESA_COMMAND*    pOrgCmd;
    MV_BUF_INFO         dmaDescBuf;
    MV_CESA_DMA         dma[MV_CESA_MAX_REQ_FRAGS];
    MV_BUF_INFO         cesaDescBuf;
    MV_CESA_DESC*       pCesaDesc;
    MV_CESA_FRAGS       frags;


} MV_CESA_REQ;


/* SRAM map */
/* Total SRAM size calculation */
/*  SRAM size =
 *              MV_CESA_MAX_BUF_SIZE  +
 *              sizeof(MV_CESA_DESC)  +
 *              MV_CESA_MAX_IV_LENGTH +
 *              MV_CESA_MAX_IV_LENGTH +
 *              MV_CESA_MAX_DIGEST_SIZE +
 *              sizeof(MV_CESA_SRAM_SA)
 *            = 1600 + 32 + 16 + 16 + 24 + 80 + 280 (reserved) = 2048 bytes
 *            = 3200 + 32 + 16 + 16 + 24 + 80 + 728 (reserved) = 4096 bytes
 */
typedef struct
{
    MV_U8               buf[MV_CESA_MAX_BUF_SIZE];
    MV_CESA_DESC        desc;
    MV_U8               cryptoIV[MV_CESA_MAX_IV_LENGTH];
    MV_U8               tempCryptoIV[MV_CESA_MAX_IV_LENGTH];
    MV_U8               tempDigest[MV_CESA_MAX_DIGEST_SIZE+4];
    MV_CESA_SRAM_SA     sramSA;

} MV_CESA_SRAM_MAP;


typedef struct
{
    MV_U32  openedCount;
    MV_U32  closedCount;
    MV_U32  fragCount;
    MV_U32  reqCount;
    MV_U32  maxReqCount;
    MV_U32  procCount;
    MV_U32  readyCount;
    MV_U32  notReadyCount;
    MV_U32  startCount;
#if (MV_CESA_VERSION >= 3)
    MV_U32  maxChainUsage;
#endif

} MV_CESA_STATS;


/* External variables */

extern MV_CESA_STATS    cesaStats;
extern MV_CESA_FRAGS    cesaFrags;

extern MV_BUF_INFO      cesaSramSaBuf;

extern MV_CESA_SA*       pCesaSAD;
extern MV_U16            cesaMaxSA;

extern MV_CESA_REQ*      pCesaReqFirst;
extern MV_CESA_REQ*      pCesaReqLast;
extern MV_CESA_REQ*      pCesaReqEmpty;
extern MV_CESA_REQ*      pCesaReqProcess;
extern int               cesaQueueDepth;
extern int               cesaReqResources;
#if (MV_CESA_VERSION>= 3)
extern MV_U32		cesaChainLength;
#endif

extern MV_CESA_SRAM_MAP*  cesaSramVirtPtr;
extern MV_U32           cesaSramPhysAddr;

static INLINE MV_ULONG  mvCesaVirtToPhys(MV_BUF_INFO* pBufInfo, void* pVirt)
{
    return (pBufInfo->bufPhysAddr + ((MV_U8*)pVirt - pBufInfo->bufVirtPtr));
}

/* Additional DEBUG functions */
void        mvCesaDebugSramSA(MV_CESA_SRAM_SA* pSramSA, int mode);
void        mvCesaDebugCmd(MV_CESA_COMMAND* pCmd,  int mode);
void        mvCesaDebugDescriptor(MV_CESA_DESC* pDesc);



#endif /* __mvCesa_h__ */