mvCesaDebug.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*******************************************************************************
  2. Copyright (C) Marvell International Ltd. and its affiliates
  3. This software file (the "File") is owned and distributed by Marvell
  4. International Ltd. and/or its affiliates ("Marvell") under the following
  5. alternative licensing terms. Once you have made an election to distribute the
  6. File under one of the following license alternatives, please (i) delete this
  7. introductory statement regarding license alternatives, (ii) delete the two
  8. license alternatives that you have not elected to use and (iii) preserve the
  9. Marvell copyright notice above.
  10. ********************************************************************************
  11. Marvell Commercial License Option
  12. If you received this File from Marvell and you have entered into a commercial
  13. license agreement (a "Commercial License") with Marvell, the File is licensed
  14. to you under the terms of the applicable Commercial License.
  15. ********************************************************************************
  16. Marvell GPL License Option
  17. If you received this File from Marvell, you may opt to use, redistribute and/or
  18. modify this File in accordance with the terms and conditions of the General
  19. Public License Version 2, June 1991 (the "GPL License"), a copy of which is
  20. available along with the File in the license.txt file or by writing to the Free
  21. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
  22. on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
  23. THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
  25. DISCLAIMED. The GPL License provides additional details about this warranty
  26. disclaimer.
  27. ********************************************************************************
  28. Marvell BSD License Option
  29. If you received this File from Marvell, you may opt to use, redistribute and/or
  30. modify this File under the following licensing terms.
  31. Redistribution and use in source and binary forms, with or without modification,
  32. are permitted provided that the following conditions are met:
  33. * Redistributions of source code must retain the above copyright notice,
  34. this list of conditions and the following disclaimer.
  35. * Redistributions in binary form must reproduce the above copyright
  36. notice, this list of conditions and the following disclaimer in the
  37. documentation and/or other materials provided with the distribution.
  38. * Neither the name of Marvell nor the names of its contributors may be
  39. used to endorse or promote products derived from this software without
  40. specific prior written permission.
  41. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  42. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  43. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  45. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  48. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  49. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  50. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51. *******************************************************************************/
  52. #include "mvOs.h"
  53. #include "mvDebug.h"
  54. #include "cesa/mvMD5.h"
  55. #include "cesa/mvSHA1.h"
  56. #include "cesa/mvCesa.h"
  57. #include "cesa/mvCesaRegs.h"
  58. #include "cesa/AES/mvAes.h"
  59. static const char* mvCesaDebugStateStr(MV_CESA_STATE state)
  60. {
  61. switch(state)
  62. {
  63. case MV_CESA_IDLE:
  64. return "Idle";
  65. case MV_CESA_PENDING:
  66. return "Pend";
  67. case MV_CESA_PROCESS:
  68. return "Proc";
  69. case MV_CESA_READY:
  70. return "Ready";
  71. default:
  72. break;
  73. }
  74. return "Unknown";
  75. }
  76. static const char* mvCesaDebugOperStr(MV_CESA_OPERATION oper)
  77. {
  78. switch(oper)
  79. {
  80. case MV_CESA_MAC_ONLY:
  81. return "MacOnly";
  82. case MV_CESA_CRYPTO_ONLY:
  83. return "CryptoOnly";
  84. case MV_CESA_MAC_THEN_CRYPTO:
  85. return "MacCrypto";
  86. case MV_CESA_CRYPTO_THEN_MAC:
  87. return "CryptoMac";
  88. default:
  89. break;
  90. }
  91. return "Null";
  92. }
  93. static const char* mvCesaDebugCryptoAlgStr(MV_CESA_CRYPTO_ALG cryptoAlg)
  94. {
  95. switch(cryptoAlg)
  96. {
  97. case MV_CESA_CRYPTO_DES:
  98. return "DES";
  99. case MV_CESA_CRYPTO_3DES:
  100. return "3DES";
  101. case MV_CESA_CRYPTO_AES:
  102. return "AES";
  103. default:
  104. break;
  105. }
  106. return "Null";
  107. }
  108. static const char* mvCesaDebugMacModeStr(MV_CESA_MAC_MODE macMode)
  109. {
  110. switch(macMode)
  111. {
  112. case MV_CESA_MAC_MD5:
  113. return "MD5";
  114. case MV_CESA_MAC_SHA1:
  115. return "SHA1";
  116. case MV_CESA_MAC_HMAC_MD5:
  117. return "HMAC-MD5";
  118. case MV_CESA_MAC_HMAC_SHA1:
  119. return "HMAC_SHA1";
  120. default:
  121. break;
  122. }
  123. return "Null";
  124. }
  125. void mvCesaDebugCmd(MV_CESA_COMMAND* pCmd, int mode)
  126. {
  127. mvOsPrintf("pCmd=%p, pReqPrv=%p, pSrc=%p, pDst=%p, pCB=%p, sid=%d\n",
  128. pCmd, pCmd->pReqPrv, pCmd->pSrc, pCmd->pDst,
  129. pCmd->pFuncCB, pCmd->sessionId);
  130. mvOsPrintf("isUser=%d, ivOffs=%d, crOffs=%d, crLen=%d, digest=%d, macOffs=%d, macLen=%d\n",
  131. pCmd->ivFromUser, pCmd->ivOffset, pCmd->cryptoOffset, pCmd->cryptoLength,
  132. pCmd->digestOffset, pCmd->macOffset, pCmd->macLength);
  133. }
  134. /* no need to use in tool */
  135. void mvCesaDebugMbuf(const char* str, MV_CESA_MBUF *pMbuf, int offset, int size)
  136. {
  137. int frag, len, fragOffset;
  138. if(str != NULL)
  139. mvOsPrintf("%s: pMbuf=%p, numFrags=%d, mbufSize=%d\n",
  140. str, pMbuf, pMbuf->numFrags, pMbuf->mbufSize);
  141. frag = mvCesaMbufOffset(pMbuf, offset, &fragOffset);
  142. if(frag == MV_INVALID)
  143. {
  144. mvOsPrintf("CESA Mbuf Error: offset (%d) out of range\n", offset);
  145. return;
  146. }
  147. for(; frag<pMbuf->numFrags; frag++)
  148. {
  149. mvOsPrintf("#%2d. bufVirt=%p, bufSize=%d\n",
  150. frag, pMbuf->pFrags[frag].bufVirtPtr,
  151. pMbuf->pFrags[frag].bufSize);
  152. if(size > 0)
  153. {
  154. len = MV_MIN(pMbuf->pFrags[frag].bufSize, size);
  155. mvDebugMemDump(pMbuf->pFrags[frag].bufVirtPtr+fragOffset, len, 1);
  156. size -= len;
  157. fragOffset = 0;
  158. }
  159. }
  160. }
  161. void mvCesaDebugRegs(void)
  162. {
  163. mvOsPrintf("\t CESA Registers:\n");
  164. mvOsPrintf("MV_CESA_CMD_REG : 0x%X = 0x%08x\n",
  165. MV_CESA_CMD_REG,
  166. MV_REG_READ( MV_CESA_CMD_REG ) );
  167. mvOsPrintf("MV_CESA_CHAN_DESC_OFFSET_REG : 0x%X = 0x%08x\n",
  168. MV_CESA_CHAN_DESC_OFFSET_REG,
  169. MV_REG_READ(MV_CESA_CHAN_DESC_OFFSET_REG) );
  170. mvOsPrintf("MV_CESA_CFG_REG : 0x%X = 0x%08x\n",
  171. MV_CESA_CFG_REG,
  172. MV_REG_READ( MV_CESA_CFG_REG ) );
  173. mvOsPrintf("MV_CESA_STATUS_REG : 0x%X = 0x%08x\n",
  174. MV_CESA_STATUS_REG,
  175. MV_REG_READ( MV_CESA_STATUS_REG ) );
  176. mvOsPrintf("MV_CESA_ISR_CAUSE_REG : 0x%X = 0x%08x\n",
  177. MV_CESA_ISR_CAUSE_REG,
  178. MV_REG_READ( MV_CESA_ISR_CAUSE_REG ) );
  179. mvOsPrintf("MV_CESA_ISR_MASK_REG : 0x%X = 0x%08x\n",
  180. MV_CESA_ISR_MASK_REG,
  181. MV_REG_READ( MV_CESA_ISR_MASK_REG ) );
  182. #if (MV_CESA_VERSION >= 2)
  183. mvOsPrintf("MV_CESA_TDMA_CTRL_REG : 0x%X = 0x%08x\n",
  184. MV_CESA_TDMA_CTRL_REG,
  185. MV_REG_READ( MV_CESA_TDMA_CTRL_REG ) );
  186. mvOsPrintf("MV_CESA_TDMA_BYTE_COUNT_REG : 0x%X = 0x%08x\n",
  187. MV_CESA_TDMA_BYTE_COUNT_REG,
  188. MV_REG_READ( MV_CESA_TDMA_BYTE_COUNT_REG ) );
  189. mvOsPrintf("MV_CESA_TDMA_SRC_ADDR_REG : 0x%X = 0x%08x\n",
  190. MV_CESA_TDMA_SRC_ADDR_REG,
  191. MV_REG_READ( MV_CESA_TDMA_SRC_ADDR_REG ) );
  192. mvOsPrintf("MV_CESA_TDMA_DST_ADDR_REG : 0x%X = 0x%08x\n",
  193. MV_CESA_TDMA_DST_ADDR_REG,
  194. MV_REG_READ( MV_CESA_TDMA_DST_ADDR_REG ) );
  195. mvOsPrintf("MV_CESA_TDMA_NEXT_DESC_PTR_REG : 0x%X = 0x%08x\n",
  196. MV_CESA_TDMA_NEXT_DESC_PTR_REG,
  197. MV_REG_READ( MV_CESA_TDMA_NEXT_DESC_PTR_REG ) );
  198. mvOsPrintf("MV_CESA_TDMA_CURR_DESC_PTR_REG : 0x%X = 0x%08x\n",
  199. MV_CESA_TDMA_CURR_DESC_PTR_REG,
  200. MV_REG_READ( MV_CESA_TDMA_CURR_DESC_PTR_REG ) );
  201. mvOsPrintf("MV_CESA_TDMA_ERROR_CAUSE_REG : 0x%X = 0x%08x\n",
  202. MV_CESA_TDMA_ERROR_CAUSE_REG,
  203. MV_REG_READ( MV_CESA_TDMA_ERROR_CAUSE_REG ) );
  204. mvOsPrintf("MV_CESA_TDMA_ERROR_MASK_REG : 0x%X = 0x%08x\n",
  205. MV_CESA_TDMA_ERROR_MASK_REG,
  206. MV_REG_READ( MV_CESA_TDMA_ERROR_CAUSE_REG ) );
  207. #endif
  208. }
  209. void mvCesaDebugStatus(void)
  210. {
  211. mvOsPrintf("\n\t CESA Status\n\n");
  212. mvOsPrintf("pReqQ=%p, qDepth=%d, reqSize=%ld bytes, qRes=%d, ",
  213. pCesaReqFirst, cesaQueueDepth, sizeof(MV_CESA_REQ),
  214. cesaReqResources);
  215. #if (MV_CESA_VERSION >= 3)
  216. mvOsPrintf("chainLength=%u\n",cesaChainLength);
  217. #else
  218. mvOsPrintf("\n");
  219. #endif
  220. mvOsPrintf("pSAD=%p, maxSA=%d, sizeSA=%ld bytes\n",
  221. pCesaSAD, cesaMaxSA, sizeof(MV_CESA_SA));
  222. mvOsPrintf("\n");
  223. mvCesaDebugRegs();
  224. mvCesaDebugStats();
  225. mvCesaDebugStatsClear();
  226. }
  227. void mvCesaDebugDescriptor(MV_CESA_DESC* pDesc)
  228. {
  229. mvOsPrintf("config=0x%08x, crSrcOffs=0x%04x, crDstOffs=0x%04x\n",
  230. pDesc->config, pDesc->cryptoSrcOffset, pDesc->cryptoDstOffset);
  231. mvOsPrintf("crLen=0x%04x, crKeyOffs=0x%04x, ivOffs=0x%04x, ivBufOffs=0x%04x\n",
  232. pDesc->cryptoDataLen, pDesc->cryptoKeyOffset,
  233. pDesc->cryptoIvOffset, pDesc->cryptoIvBufOffset);
  234. mvOsPrintf("macSrc=0x%04x, digest=0x%04x, macLen=0x%04x, inIv=0x%04x, outIv=0x%04x\n",
  235. pDesc->macSrcOffset, pDesc->macDigestOffset, pDesc->macDataLen,
  236. pDesc->macInnerIvOffset, pDesc->macOuterIvOffset);
  237. }
  238. void mvCesaDebugQueue(int mode)
  239. {
  240. mvOsPrintf("\n\t CESA Request Queue:\n\n");
  241. mvOsPrintf("pFirstReq=%p, pLastReq=%p, qDepth=%d, reqSize=%ld bytes\n",
  242. pCesaReqFirst, pCesaReqLast, cesaQueueDepth, sizeof(MV_CESA_REQ));
  243. mvOsPrintf("pEmpty=%p, pProcess=%p, qResources=%d\n",
  244. pCesaReqEmpty, pCesaReqProcess,
  245. cesaReqResources);
  246. if(mode != 0)
  247. {
  248. int count = 0;
  249. MV_CESA_REQ* pReq = pCesaReqFirst;
  250. for(count=0; count<cesaQueueDepth; count++)
  251. {
  252. /* Print out requsts */
  253. mvOsPrintf("%02d. pReq=%p, state=%s, frag=0x%x, pCmd=%p, pDma=%p, pDesc=%p\n",
  254. count, pReq, mvCesaDebugStateStr(pReq->state),
  255. pReq->fragMode, pReq->pCmd, pReq->dma[0].pDmaFirst, &pReq->pCesaDesc[0]);
  256. if(pReq->fragMode != MV_CESA_FRAG_NONE)
  257. {
  258. int frag;
  259. mvOsPrintf("pFrags=%p, num=%d, next=%d, bufOffset=%d, cryptoSize=%d, macSize=%d\n",
  260. &pReq->frags, pReq->frags.numFrag, pReq->frags.nextFrag,
  261. pReq->frags.bufOffset, pReq->frags.cryptoSize, pReq->frags.macSize);
  262. for(frag=0; frag<pReq->frags.numFrag; frag++)
  263. {
  264. mvOsPrintf("#%d: pDmaFirst=%p, pDesc=%p\n", frag,
  265. pReq->dma[frag].pDmaFirst, &pReq->pCesaDesc[frag]);
  266. }
  267. }
  268. if(mode > 1)
  269. {
  270. /* Print out Command */
  271. mvCesaDebugCmd(pReq->pCmd, mode);
  272. /* Print out Descriptor */
  273. mvCesaDebugDescriptor(&pReq->pCesaDesc[0]);
  274. }
  275. pReq++;
  276. }
  277. }
  278. }
  279. void mvCesaDebugSramSA(MV_CESA_SRAM_SA* pSramSA, int mode)
  280. {
  281. if(pSramSA == NULL)
  282. {
  283. mvOsPrintf("cesaSramSA: Unexpected pSramSA=%p\n", pSramSA);
  284. return;
  285. }
  286. mvOsPrintf("pSramSA=%p, sizeSramSA=%ld bytes\n",
  287. pSramSA, sizeof(MV_CESA_SRAM_SA));
  288. if(mode != 0)
  289. {
  290. mvOsPrintf("cryptoKey=%p, maxCryptoKey=%d bytes\n",
  291. pSramSA->cryptoKey, MV_CESA_MAX_CRYPTO_KEY_LENGTH);
  292. mvDebugMemDump(pSramSA->cryptoKey, MV_CESA_MAX_CRYPTO_KEY_LENGTH, 1);
  293. mvOsPrintf("macInnerIV=%p, maxInnerIV=%d bytes\n",
  294. pSramSA->macInnerIV, MV_CESA_MAX_DIGEST_SIZE);
  295. mvDebugMemDump(pSramSA->macInnerIV, MV_CESA_MAX_DIGEST_SIZE, 1);
  296. mvOsPrintf("macOuterIV=%p, maxOuterIV=%d bytes\n",
  297. pSramSA->macOuterIV, MV_CESA_MAX_DIGEST_SIZE);
  298. mvDebugMemDump(pSramSA->macOuterIV, MV_CESA_MAX_DIGEST_SIZE, 1);
  299. }
  300. }
  301. void mvCesaDebugSA(short sid, int mode)
  302. {
  303. MV_CESA_OPERATION oper;
  304. MV_CESA_DIRECTION dir;
  305. MV_CESA_CRYPTO_ALG cryptoAlg;
  306. MV_CESA_CRYPTO_MODE cryptoMode;
  307. MV_CESA_MAC_MODE macMode;
  308. MV_CESA_SA* pSA = &pCesaSAD[sid];
  309. if( (pSA->valid) || ((pSA->count != 0) && (mode > 0)) || (mode >= 2) )
  310. {
  311. mvOsPrintf("\n\nCESA SA Entry #%d (%p) - %s (count=%d)\n",
  312. sid, pSA,
  313. pSA->valid ? "Valid" : "Invalid", pSA->count);
  314. oper = (pSA->config & MV_CESA_OPERATION_MASK) >> MV_CESA_OPERATION_OFFSET;
  315. dir = (pSA->config & MV_CESA_DIRECTION_MASK) >> MV_CESA_DIRECTION_BIT;
  316. mvOsPrintf("%s - %s ", mvCesaDebugOperStr(oper),
  317. (dir == MV_CESA_DIR_ENCODE) ? "Encode" : "Decode");
  318. if(oper != MV_CESA_MAC_ONLY)
  319. {
  320. cryptoAlg = (pSA->config & MV_CESA_CRYPTO_ALG_MASK) >> MV_CESA_CRYPTO_ALG_OFFSET;
  321. cryptoMode = (pSA->config & MV_CESA_CRYPTO_MODE_MASK) >> MV_CESA_CRYPTO_MODE_BIT;
  322. mvOsPrintf("- %s - %s ", mvCesaDebugCryptoAlgStr(cryptoAlg),
  323. (cryptoMode == MV_CESA_CRYPTO_ECB) ? "ECB" : "CBC");
  324. }
  325. if(oper != MV_CESA_CRYPTO_ONLY)
  326. {
  327. macMode = (pSA->config & MV_CESA_MAC_MODE_MASK) >> MV_CESA_MAC_MODE_OFFSET;
  328. mvOsPrintf("- %s ", mvCesaDebugMacModeStr(macMode));
  329. }
  330. mvOsPrintf("\n");
  331. if(mode > 0)
  332. {
  333. mvOsPrintf("config=0x%08x, cryptoKeySize=%d, digestSize=%d\n",
  334. pCesaSAD[sid].config, pCesaSAD[sid].cryptoKeyLength,
  335. pCesaSAD[sid].digestSize);
  336. mvCesaDebugSramSA(pCesaSAD[sid].pSramSA, mode);
  337. }
  338. }
  339. }
  340. /**/
  341. void mvCesaDebugSram(int mode)
  342. {
  343. mvOsPrintf("\n\t SRAM contents: size=%ld, pVirt=%p\n\n",
  344. sizeof(MV_CESA_SRAM_MAP), cesaSramVirtPtr);
  345. mvOsPrintf("\n\t Sram buffer: size=%d, pVirt=%p\n",
  346. MV_CESA_MAX_BUF_SIZE, cesaSramVirtPtr->buf);
  347. if(mode != 0)
  348. mvDebugMemDump(cesaSramVirtPtr->buf, 64, 1);
  349. mvOsPrintf("\n");
  350. mvOsPrintf("\n\t Sram descriptor: size=%ld, pVirt=%p\n",
  351. sizeof(MV_CESA_DESC), &cesaSramVirtPtr->desc);
  352. if(mode != 0)
  353. {
  354. mvOsPrintf("\n");
  355. mvCesaDebugDescriptor(&cesaSramVirtPtr->desc);
  356. }
  357. mvOsPrintf("\n\t Sram IV: size=%d, pVirt=%p\n",
  358. MV_CESA_MAX_IV_LENGTH, &cesaSramVirtPtr->cryptoIV);
  359. if(mode != 0)
  360. {
  361. mvOsPrintf("\n");
  362. mvDebugMemDump(cesaSramVirtPtr->cryptoIV, MV_CESA_MAX_IV_LENGTH, 1);
  363. }
  364. mvOsPrintf("\n");
  365. mvCesaDebugSramSA(&cesaSramVirtPtr->sramSA, 0);
  366. }
  367. void mvCesaDebugSAD(int mode)
  368. {
  369. int sid;
  370. mvOsPrintf("\n\t Cesa SAD status: pSAD=%p, maxSA=%d\n",
  371. pCesaSAD, cesaMaxSA);
  372. for(sid=0; sid<cesaMaxSA; sid++)
  373. {
  374. mvCesaDebugSA(sid, mode);
  375. }
  376. }
  377. void mvCesaDebugStats(void)
  378. {
  379. mvOsPrintf("\n\t Cesa Statistics\n");
  380. mvOsPrintf("Opened=%u, Closed=%u\n",
  381. cesaStats.openedCount, cesaStats.closedCount);
  382. mvOsPrintf("Req=%u, maxReq=%u, frags=%u, start=%u\n",
  383. cesaStats.reqCount, cesaStats.maxReqCount,
  384. cesaStats.fragCount, cesaStats.startCount);
  385. #if (MV_CESA_VERSION >= 3)
  386. mvOsPrintf("maxChainUsage=%u\n",cesaStats.maxChainUsage);
  387. #endif
  388. mvOsPrintf("\n");
  389. mvOsPrintf("proc=%u, ready=%u, notReady=%u\n",
  390. cesaStats.procCount, cesaStats.readyCount, cesaStats.notReadyCount);
  391. }
  392. void mvCesaDebugStatsClear(void)
  393. {
  394. memset(&cesaStats, 0, sizeof(cesaStats));
  395. }