summaryrefslogtreecommitdiffstats
path: root/target/linux/generic-2.6/files/crypto/ocf/kirkwood/cesa/mvCesaDebug.c
blob: 31b78a8053f4dd2e929af3e61d62ea19e28f133a (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*******************************************************************************
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.

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

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

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

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

static const char*   mvCesaDebugStateStr(MV_CESA_STATE state)
{
    switch(state)
    {
        case MV_CESA_IDLE:
            return "Idle";

        case MV_CESA_PENDING:
            return "Pend";

        case MV_CESA_PROCESS:
            return "Proc";

        case MV_CESA_READY:
            return "Ready";

        default:
            break;
    }
    return "Unknown";
}

static const char*   mvCesaDebugOperStr(MV_CESA_OPERATION oper)
{
    switch(oper)
    {
        case MV_CESA_MAC_ONLY:
            return "MacOnly";

        case MV_CESA_CRYPTO_ONLY:
            return "CryptoOnly";

        case MV_CESA_MAC_THEN_CRYPTO:
            return "MacCrypto";

        case MV_CESA_CRYPTO_THEN_MAC:
            return "CryptoMac";

        default:
            break;
    }
    return "Null";
}

static const char* mvCesaDebugCryptoAlgStr(MV_CESA_CRYPTO_ALG cryptoAlg)
{
    switch(cryptoAlg)
    {
        case MV_CESA_CRYPTO_DES:
            return "DES";

        case MV_CESA_CRYPTO_3DES:
            return "3DES";

        case MV_CESA_CRYPTO_AES:
            return "AES";

        default:
            break;
    }
    return "Null";
}

static const char* mvCesaDebugMacModeStr(MV_CESA_MAC_MODE macMode)
{
    switch(macMode)
    {
        case MV_CESA_MAC_MD5:
            return "MD5";

        case MV_CESA_MAC_SHA1:
            return "SHA1";

        case MV_CESA_MAC_HMAC_MD5:
            return "HMAC-MD5";

        case MV_CESA_MAC_HMAC_SHA1:
            return "HMAC_SHA1";

        default:
            break;
    }
    return "Null";
}

void    mvCesaDebugCmd(MV_CESA_COMMAND* pCmd,  int mode)
{
    mvOsPrintf("pCmd=%p, pReqPrv=%p, pSrc=%p, pDst=%p, pCB=%p, sid=%d\n",
                pCmd, pCmd->pReqPrv, pCmd->pSrc, pCmd->pDst,
                pCmd->pFuncCB, pCmd->sessionId);
    mvOsPrintf("isUser=%d, ivOffs=%d, crOffs=%d, crLen=%d, digest=%d, macOffs=%d, macLen=%d\n",
                pCmd->ivFromUser, pCmd->ivOffset, pCmd->cryptoOffset, pCmd->cryptoLength,
                pCmd->digestOffset, pCmd->macOffset, pCmd->macLength);
}

/* no need to use in tool */
void     mvCesaDebugMbuf(const char* str, MV_CESA_MBUF *pMbuf, int offset, int size)
{
    int frag, len, fragOffset;

    if(str != NULL)
        mvOsPrintf("%s: pMbuf=%p, numFrags=%d, mbufSize=%d\n",
                    str, pMbuf, pMbuf->numFrags, pMbuf->mbufSize);

    frag = mvCesaMbufOffset(pMbuf, offset, &fragOffset);
    if(frag == MV_INVALID)
    {
        mvOsPrintf("CESA Mbuf Error: offset (%d) out of range\n", offset);
        return;
    }

    for(; frag<pMbuf->numFrags; frag++)
    {
        mvOsPrintf("#%2d. bufVirt=%p, bufSize=%d\n",
                    frag, pMbuf->pFrags[frag].bufVirtPtr,
                    pMbuf->pFrags[frag].bufSize);
        if(size > 0)
        {
            len = MV_MIN(pMbuf->pFrags[frag].bufSize, size);
            mvDebugMemDump(pMbuf->pFrags[frag].bufVirtPtr+fragOffset, len, 1);
            size -= len;
            fragOffset = 0;
        }
    }
}

void    mvCesaDebugRegs(void)
{
    mvOsPrintf("\t CESA Registers:\n");

    mvOsPrintf("MV_CESA_CMD_REG                     : 0x%X = 0x%08x\n",
                MV_CESA_CMD_REG,
                MV_REG_READ( MV_CESA_CMD_REG ) );

    mvOsPrintf("MV_CESA_CHAN_DESC_OFFSET_REG        : 0x%X = 0x%08x\n",
                MV_CESA_CHAN_DESC_OFFSET_REG,
                MV_REG_READ(MV_CESA_CHAN_DESC_OFFSET_REG) );

    mvOsPrintf("MV_CESA_CFG_REG                     : 0x%X = 0x%08x\n",
                MV_CESA_CFG_REG,
                MV_REG_READ( MV_CESA_CFG_REG ) );

    mvOsPrintf("MV_CESA_STATUS_REG                  : 0x%X = 0x%08x\n",
                MV_CESA_STATUS_REG,
                MV_REG_READ( MV_CESA_STATUS_REG ) );

    mvOsPrintf("MV_CESA_ISR_CAUSE_REG               : 0x%X = 0x%08x\n",
                MV_CESA_ISR_CAUSE_REG,
                MV_REG_READ( MV_CESA_ISR_CAUSE_REG ) );

    mvOsPrintf("MV_CESA_ISR_MASK_REG                : 0x%X = 0x%08x\n",
                MV_CESA_ISR_MASK_REG,
                MV_REG_READ( MV_CESA_ISR_MASK_REG ) );
#if (MV_CESA_VERSION >= 2)
    mvOsPrintf("MV_CESA_TDMA_CTRL_REG               : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_CTRL_REG, 
                MV_REG_READ( MV_CESA_TDMA_CTRL_REG ) );

    mvOsPrintf("MV_CESA_TDMA_BYTE_COUNT_REG         : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_BYTE_COUNT_REG, 
                MV_REG_READ( MV_CESA_TDMA_BYTE_COUNT_REG ) );

    mvOsPrintf("MV_CESA_TDMA_SRC_ADDR_REG           : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_SRC_ADDR_REG, 
                MV_REG_READ( MV_CESA_TDMA_SRC_ADDR_REG ) );

    mvOsPrintf("MV_CESA_TDMA_DST_ADDR_REG           : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_DST_ADDR_REG, 
                MV_REG_READ( MV_CESA_TDMA_DST_ADDR_REG ) );

    mvOsPrintf("MV_CESA_TDMA_NEXT_DESC_PTR_REG      : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_NEXT_DESC_PTR_REG, 
                MV_REG_READ( MV_CESA_TDMA_NEXT_DESC_PTR_REG ) );

    mvOsPrintf("MV_CESA_TDMA_CURR_DESC_PTR_REG      : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_CURR_DESC_PTR_REG, 
                MV_REG_READ( MV_CESA_TDMA_CURR_DESC_PTR_REG ) );

    mvOsPrintf("MV_CESA_TDMA_ERROR_CAUSE_REG        : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_ERROR_CAUSE_REG, 
                MV_REG_READ( MV_CESA_TDMA_ERROR_CAUSE_REG ) );

    mvOsPrintf("MV_CESA_TDMA_ERROR_MASK_REG         : 0x%X = 0x%08x\n", 
                MV_CESA_TDMA_ERROR_MASK_REG, 
                MV_REG_READ( MV_CESA_TDMA_ERROR_CAUSE_REG ) );

#endif
}

void    mvCesaDebugStatus(void)
{
    mvOsPrintf("\n\t CESA Status\n\n");

    mvOsPrintf("pReqQ=%p, qDepth=%d, reqSize=%ld bytes, qRes=%d, ",
                pCesaReqFirst, cesaQueueDepth, sizeof(MV_CESA_REQ),
                cesaReqResources);
#if (MV_CESA_VERSION >= 3)
    mvOsPrintf("chainLength=%u\n",cesaChainLength);
#else
   mvOsPrintf("\n"); 	
#endif

    mvOsPrintf("pSAD=%p, maxSA=%d, sizeSA=%ld bytes\n",
                pCesaSAD, cesaMaxSA, sizeof(MV_CESA_SA));

    mvOsPrintf("\n");

    mvCesaDebugRegs();
    mvCesaDebugStats();
    mvCesaDebugStatsClear();
}

void    mvCesaDebugDescriptor(MV_CESA_DESC* pDesc)
{
    mvOsPrintf("config=0x%08x, crSrcOffs=0x%04x, crDstOffs=0x%04x\n",
            pDesc->config, pDesc->cryptoSrcOffset, pDesc->cryptoDstOffset);

    mvOsPrintf("crLen=0x%04x, crKeyOffs=0x%04x, ivOffs=0x%04x, ivBufOffs=0x%04x\n",
            pDesc->cryptoDataLen, pDesc->cryptoKeyOffset,
            pDesc->cryptoIvOffset, pDesc->cryptoIvBufOffset);

    mvOsPrintf("macSrc=0x%04x, digest=0x%04x, macLen=0x%04x, inIv=0x%04x, outIv=0x%04x\n",
            pDesc->macSrcOffset, pDesc->macDigestOffset, pDesc->macDataLen,
            pDesc->macInnerIvOffset, pDesc->macOuterIvOffset);
}

void    mvCesaDebugQueue(int mode)
{
    mvOsPrintf("\n\t CESA Request Queue:\n\n");

    mvOsPrintf("pFirstReq=%p, pLastReq=%p, qDepth=%d, reqSize=%ld bytes\n",
                pCesaReqFirst, pCesaReqLast, cesaQueueDepth, sizeof(MV_CESA_REQ));

    mvOsPrintf("pEmpty=%p, pProcess=%p, qResources=%d\n",
                pCesaReqEmpty, pCesaReqProcess,
                cesaReqResources);

    if(mode != 0)
    {
        int             count = 0;
        MV_CESA_REQ*    pReq = pCesaReqFirst;

        for(count=0; count<cesaQueueDepth; count++)
        {
            /* Print out requsts */
            mvOsPrintf("%02d. pReq=%p, state=%s, frag=0x%x, pCmd=%p, pDma=%p, pDesc=%p\n",
                count, pReq, mvCesaDebugStateStr(pReq->state),
                pReq->fragMode, pReq->pCmd, pReq->dma[0].pDmaFirst, &pReq->pCesaDesc[0]);
            if(pReq->fragMode != MV_CESA_FRAG_NONE)
            {
                int frag;

                mvOsPrintf("pFrags=%p, num=%d, next=%d, bufOffset=%d, cryptoSize=%d, macSize=%d\n",
                            &pReq->frags, pReq->frags.numFrag, pReq->frags.nextFrag,
                            pReq->frags.bufOffset, pReq->frags.cryptoSize, pReq->frags.macSize);
                for(frag=0; frag<pReq->frags.numFrag; frag++)
                {
                    mvOsPrintf("#%d: pDmaFirst=%p, pDesc=%p\n", frag,
                                pReq->dma[frag].pDmaFirst, &pReq->pCesaDesc[frag]);
                }
            }
            if(mode > 1)
            {
                /* Print out Command */
                mvCesaDebugCmd(pReq->pCmd, mode);

                /* Print out Descriptor */
                mvCesaDebugDescriptor(&pReq->pCesaDesc[0]);
            }
            pReq++;
        }
    }
}


void    mvCesaDebugSramSA(MV_CESA_SRAM_SA* pSramSA, int mode)
{
    if(pSramSA == NULL)
    {
        mvOsPrintf("cesaSramSA: Unexpected pSramSA=%p\n", pSramSA);
        return;
    }
    mvOsPrintf("pSramSA=%p, sizeSramSA=%ld bytes\n",
                pSramSA, sizeof(MV_CESA_SRAM_SA));

    if(mode != 0)
    {
        mvOsPrintf("cryptoKey=%p, maxCryptoKey=%d bytes\n",
                    pSramSA->cryptoKey, MV_CESA_MAX_CRYPTO_KEY_LENGTH);
        mvDebugMemDump(pSramSA->cryptoKey, MV_CESA_MAX_CRYPTO_KEY_LENGTH, 1);

        mvOsPrintf("macInnerIV=%p, maxInnerIV=%d bytes\n",
                    pSramSA->macInnerIV, MV_CESA_MAX_DIGEST_SIZE);
        mvDebugMemDump(pSramSA->macInnerIV, MV_CESA_MAX_DIGEST_SIZE, 1);

        mvOsPrintf("macOuterIV=%p, maxOuterIV=%d bytes\n",
                    pSramSA->macOuterIV, MV_CESA_MAX_DIGEST_SIZE);
        mvDebugMemDump(pSramSA->macOuterIV, MV_CESA_MAX_DIGEST_SIZE, 1);
    }
}

void    mvCesaDebugSA(short sid, int mode)
{
    MV_CESA_OPERATION   oper;
    MV_CESA_DIRECTION   dir;
    MV_CESA_CRYPTO_ALG  cryptoAlg;
    MV_CESA_CRYPTO_MODE cryptoMode;
    MV_CESA_MAC_MODE    macMode;
    MV_CESA_SA*         pSA = &pCesaSAD[sid];

    if( (pSA->valid) || ((pSA->count != 0) && (mode > 0)) || (mode >= 2) )
    {
        mvOsPrintf("\n\nCESA SA Entry #%d (%p) - %s (count=%d)\n",
                    sid, pSA,
                    pSA->valid ? "Valid" : "Invalid", pSA->count);

        oper = (pSA->config & MV_CESA_OPERATION_MASK) >> MV_CESA_OPERATION_OFFSET;
        dir  = (pSA->config & MV_CESA_DIRECTION_MASK) >> MV_CESA_DIRECTION_BIT;
        mvOsPrintf("%s - %s ", mvCesaDebugOperStr(oper),
                    (dir == MV_CESA_DIR_ENCODE) ? "Encode" : "Decode");
        if(oper != MV_CESA_MAC_ONLY)
        {
            cryptoAlg = (pSA->config & MV_CESA_CRYPTO_ALG_MASK) >> MV_CESA_CRYPTO_ALG_OFFSET;
            cryptoMode = (pSA->config & MV_CESA_CRYPTO_MODE_MASK) >> MV_CESA_CRYPTO_MODE_BIT;
            mvOsPrintf("- %s - %s ", mvCesaDebugCryptoAlgStr(cryptoAlg),
                        (cryptoMode == MV_CESA_CRYPTO_ECB) ? "ECB" : "CBC");
        }
        if(oper != MV_CESA_CRYPTO_ONLY)
        {
            macMode = (pSA->config & MV_CESA_MAC_MODE_MASK) >> MV_CESA_MAC_MODE_OFFSET;
            mvOsPrintf("- %s ", mvCesaDebugMacModeStr(macMode));
        }
        mvOsPrintf("\n");

        if(mode > 0)
        {
            mvOsPrintf("config=0x%08x, cryptoKeySize=%d, digestSize=%d\n",
                        pCesaSAD[sid].config, pCesaSAD[sid].cryptoKeyLength,
                        pCesaSAD[sid].digestSize);

            mvCesaDebugSramSA(pCesaSAD[sid].pSramSA, mode);
        }
    }
}


/**/
void    mvCesaDebugSram(int mode)
{
    mvOsPrintf("\n\t SRAM contents: size=%ld, pVirt=%p\n\n",
            sizeof(MV_CESA_SRAM_MAP), cesaSramVirtPtr);

    mvOsPrintf("\n\t Sram buffer: size=%d, pVirt=%p\n",
                    MV_CESA_MAX_BUF_SIZE, cesaSramVirtPtr->buf);
        if(mode != 0)
            mvDebugMemDump(cesaSramVirtPtr->buf, 64, 1);

    mvOsPrintf("\n");
    mvOsPrintf("\n\t Sram descriptor: size=%ld, pVirt=%p\n",
                    sizeof(MV_CESA_DESC), &cesaSramVirtPtr->desc);
    if(mode != 0)
    {
        mvOsPrintf("\n");
        mvCesaDebugDescriptor(&cesaSramVirtPtr->desc);
    }
    mvOsPrintf("\n\t Sram IV: size=%d, pVirt=%p\n",
                    MV_CESA_MAX_IV_LENGTH, &cesaSramVirtPtr->cryptoIV);
    if(mode != 0)
    {
        mvOsPrintf("\n");
        mvDebugMemDump(cesaSramVirtPtr->cryptoIV, MV_CESA_MAX_IV_LENGTH, 1);
    }
    mvOsPrintf("\n");
    mvCesaDebugSramSA(&cesaSramVirtPtr->sramSA, 0);
}

void    mvCesaDebugSAD(int mode)
{
    int sid;

    mvOsPrintf("\n\t Cesa SAD status: pSAD=%p, maxSA=%d\n",
                pCesaSAD, cesaMaxSA);

    for(sid=0; sid<cesaMaxSA; sid++)
    {
        mvCesaDebugSA(sid, mode);
    }
}

void    mvCesaDebugStats(void)
{
    mvOsPrintf("\n\t Cesa Statistics\n");

    mvOsPrintf("Opened=%u, Closed=%u\n",
                cesaStats.openedCount, cesaStats.closedCount);
    mvOsPrintf("Req=%u, maxReq=%u, frags=%u, start=%u\n",
                cesaStats.reqCount, cesaStats.maxReqCount,
                cesaStats.fragCount, cesaStats.startCount);
#if (MV_CESA_VERSION >= 3)
    mvOsPrintf("maxChainUsage=%u\n",cesaStats.maxChainUsage);
#endif
    mvOsPrintf("\n");
    mvOsPrintf("proc=%u, ready=%u, notReady=%u\n",
                cesaStats.procCount, cesaStats.readyCount, cesaStats.notReadyCount);
}

void    mvCesaDebugStatsClear(void)
{
    memset(&cesaStats, 0, sizeof(cesaStats));
}