1 /**
2 * @file salldsim.c
3 *
4 * @brief
5 * This is the SA Low Level Driver unit test file.
6 *
7 * \par
8 * ============================================================================
9 * @n (C) Copyright 2009-2020, Texas Instruments, Inc.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the
21 * distribution.
22 *
23 * Neither the name of Texas Instruments Incorporated nor the names of
24 * its contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 */
41 #include "../unittest.h"
42 #include "salldcfg.h"
43 #include <ti/csl/cslr_cp_ace.h>
45 #ifdef SA_3GPP_SUPPORT
46 #include <ti/drv/sa/sa3gppEnabler/sa3gpp.h>
47 #endif
49 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
50 /* SA control register for debugging purpose only */
51 static CSL_Cp_aceRegs* pSaRegs = (CSL_Cp_aceRegs*)CSL_NETCP_CFG_SA_CFG_REGS;
52 #endif
54 extern Bool saTestLogEnable;
57 /* Global booleans */
58 volatile Bool salldSimHalt;
60 /* Global eror code */
61 salldSimErr_e salldSimErrorId;
63 /* Gobal system timer variable */
64 uint32_t systemTime = 0;
65 uint16_t prevTime = 0;
67 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
68 volatile uint32_t* pSAModelTrig = (volatile uint32_t*)(CSL_NETCP_CFG_SA_CFG_REGS + 0x4000);
69 #endif
71 /* Global char signifying entity calling salldsimDebug() function */
72 char salldsimDebugString = 0;
74 /* Global char signifying entity calling salldsimFatalError() function */
75 char salldsimFatalString = 0;
77 char err_string[1];
79 /* Global string for logfile printing */
80 char print_string[salld_SIM_MAX_STRING];
82 int numSalldSimChans;
84 /* Simulation configuration structure & pointer */
85 simConfig_t simConfig;
86 simConfig_t *simCfg = &simConfig;
88 /* Global SALLD call-out function table */
89 static Sa_CallOutFuncs_t salldsim_calloutFunc = {
90 (void (*)(Sa_ChanHandle, uint16_t, uint16_t, uint16_t, uint16_t *)) salldsimDebugInfo, /* Debug function pointer */
91 (void (*)(Sa_ChanHandle, Sa_KeyRequest_t *))salldsimChanKeyRequest, /* Key Request function Pointer */
92 (void (*)(Sa_ChanHandle handle, Sa_ScReqInfo_t* scReqInfo))salldsimScAlloc, /* Security Context Allocation function pointer */
93 (void (*)(Sa_ChanHandle handle, uint16_t scID))salldsimScFree, /* Security Context Free Function pointer */
94 (void (*)(Sa_ChanHandle handle, Sa_SWInfo_t* chanSwInfo))salldsimChanRegister, /* Channel Registration Function pointer */
95 (void (*)(Sa_ChanHandle handle, Sa_SWInfo_t* chanSwInfo))salldsimChanUnRegister, /* Channel UnRegister Function pointer */
96 (void (*)(Sa_ChanHandle handle, Sa_PktInfo_t *pktInfo))salldsimChanSendNullPkt /* Channel Send Null Packet function pointer */
97 };
100 #define SALLDSIM_GET_CHAN(id) ((id) & 0x00FF)
102 extern void sauReregegisterUdp(void* pHdl);
104 /*Internal Memory Scratch Buffer */
105 #ifdef _TMS320C6X
106 #pragma DATA_SECTION(gIntBuf, ".intMem")
107 #endif
108 uint8_t gIntBuf[SALLDSIM_SC_BUF_SIZE];
111 /* Define SA related base address */
112 void mdebugHaltSaPdsp (Int pdspNum)
113 {
114 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
115 pSaRegs->PDSP_CONTROL_STATUS[pdspNum].PDSP_CONTROL &= ~(CSL_CP_ACE_PDSP_CONTROL_ENABLE_MASK);
116 #endif
118 }
120 /*-----------------------------------------------------------------
121 * Function: salld_sim_halt
122 *
123 * Description: Utility to provide simulation breakpoint.
124 *-----------------------------------------------------------------*/
125 void salld_sim_halt (void)
126 {
127 /* Wait for further actions - exit when user sets "halt" to FALSE */
128 while (salldSimHalt) {
129 asm (" NOP");
130 asm (" NOP");
131 asm (" NOP");
132 asm (" NOP");
133 }
134 /* Reset "halt" to TRUE for next entrance */
135 salldSimHalt = TRUE;
136 }
138 /******************************************************************************
139 * FUNCTION PURPOSE: Generic function for handling SALLD exception calls.
140 ******************************************************************************
141 * DESCRIPTION: Handles exception and debug calls. Goes into and infinite loop,
142 * allowing for an eventual break and examintaaion of reason for
143 * calling a fata error. The global variable testFatalString
144 * contains the reason for entry, as enumerated in the global
145 * enumeration testFatalErrorChar_e of file sa_lld_test.h.
146 *
147 * CALLING FORMAT:
148 * testFatalError (uint16_t ID, - ID and channel of calling entity
149 * (either FM or DPU)
150 * char* str) - Global ptr to char that holds the
151 * value of reason for entering
152 * this function
153 *
154 * RETURN VALUE:
155 * None
156 *
157 *****************************************************************************/
158 void salldsimFatalError (uint16_t id, char* str)
159 {
160 salld_sim_print("SALLDSIM %d: %s \n", id, str);
161 salld_sim_halt ();
162 }
164 \f
165 /******************************************************************************
166 * FUNCTION PURPOSE: Implements SALLD's debugInfo() API call.
167 ******************************************************************************
168 * DESCRIPTION: Handles SALLD API calls to debugInfo(). All INFORMATIONAL msgs
169 * are ignored; if the msgType is FATAL_ERROR or undefined,
170 * a call to salldsimFatalError() is made.
171 *
172 * CALLING FORMAT:
173 * salldsimSalldDebugInfo (void * moduleID, - ID and channel of FIU
174 * uint16_t msgType, - FATAL_ERROR (2) or
175 * INFORMATIONAL (0)
176 * uint16_t messageCode, - not used
177 * uint16_t msgLength, - not used
178 * uint16_t *supportingData)- not used
179 *
180 * RETURN VALUES:
181 * None
182 *
183 *****************************************************************************/
184 void salldsimDebugInfo (void* mID, uint16_t msgType, uint16_t messageCode,
185 uint16_t msgLength, uint16_t *supportingData)
186 {
188 uint16_t id = Sa_chanGetID((Sa_ChanHandle) mID);
189 int chnum = SALLDSIM_GET_CHAN(id);
190 char *string = (char *)(uintptr_t)NULL;
191 char msg_default[80] = "TEST_EXCEPTION_DBG_DEFAULT";
192 char msg_fatal_err[80] = "TEST_EXCEPTION_DBG_FATAL_ERROR";
193 uint16_t time = 0;
194 uint16_t deltaTime = 0;
196 switch (msgType)
197 {
198 case sa_DBG_INFORMATIONAL:
199 if (msgLength == 2)
200 {
201 time = supportingData[1];
203 if (time >= prevTime)
204 deltaTime = time - prevTime;
205 else
206 deltaTime = (0xFFFF - prevTime + 1) + time;
208 prevTime = time;
209 }
211 salld_sim_print ("%.4X(+%.4dms)GW%d:SALLD~ ", time, deltaTime,chnum);
212 salld_sim_print (":msgId = 0x%X, msgData = 0x%X\n", messageCode, *supportingData);
213 break;
215 case sa_DBG_FATAL_ERROR:
216 string = msg_fatal_err;
217 salldsimFatalError (id, string);
218 break;
220 default:
221 string = msg_default;
222 salldsimFatalError (id, string);
223 break;
224 }
225 } /* salldsimSalldDebugInfo */
228 /**
229 * @brief Callout to externally supplied system to request a new security key. This function may be triggered
230 * by either the sslldChanSendData() or sslldChanReceiveData() APIs. The application should call the
231 * Sa_chanControl() API to pass the new key when it is available.
232 * This is a function pointer and must point to a valid function location who meets the API requirements.
233 *
234 * @param[in] handle SALLD channel instance identifier.
235 * @param[in] keyReq Pointer to SALLD key Request structure.
236 *
237 * @sa Sa_KeyRequest_t
238 *
239 */
240 void salldsimChanKeyRequest (Sa_ChanHandle handle, Sa_KeyRequest_t* keyReq)
241 {
242 uint16_t id = Sa_chanGetID(handle);
243 int chnum = SALLDSIM_GET_CHAN(id);
244 #if defined(SAU_PRMOTE_DEMOTE_TEST)
245 salldSimChannel_t *pChan = &tFrameworkSecure.salldSimChn[chnum - 1];
246 #else
247 salldSimChannel_t *pChan = &tFramework.salldSimChn[chnum - 1];
248 #endif
249 if (keyReq->params.srtp.ctrlBitfield & sa_SRTP_KEY_REQUEST_TX_KEY)
250 {
251 pChan->txInfo.keyReq = *keyReq;
252 pChan->stateBitMap |= salld_SIM_STATE_TX_REKEY;
253 }
255 if (keyReq->params.srtp.ctrlBitfield & sa_SRTP_KEY_REQUEST_RX_KEY)
256 {
257 pChan->rxInfo.keyReq = *keyReq;
258 pChan->stateBitMap |= salld_SIM_STATE_RX_REKEY;
259 }
261 salld_sim_print("SALLD Channel %d: Key Request with ctrlBitfield = 0x%04x\n", chnum - 1, keyReq->params.srtp.ctrlBitfield);
262 }
264 /**
265 * @brief Callout to externally supplied system to allocate the security context with the specified size.
266 * This function must be implemented as a simple non-blocking function.
267 * This is a function pointer and must point to a valid function location who meets the API requirements.
268 *
269 * @param[in] handle SALLD channel instance identifier.
270 * @param[in] scReqInfo Pointer to SALLD security context Control Information structure.
271 *
272 * @sa Sa_ScReqInfo_t
273 *
274 */
275 void salldsimScAlloc (Sa_ChanHandle handle, Sa_ScReqInfo_t* scReqInfo)
276 {
277 uint16_t id = Sa_chanGetID(handle);
278 int chnum = SALLDSIM_GET_CHAN(id);
279 #if defined(SAU_PRMOTE_DEMOTE_TEST)
280 salldSimChannel_t *pChan = &tFrameworkSecure.salldSimChn[chnum - 1];
281 #else
282 salldSimChannel_t *pChan = &tFramework.salldSimChn[chnum - 1];
283 #endif
285 int index;
287 if(salld_sim_alloc_sc(scReqInfo->scSize, &scReqInfo->scID, (uint8_t **) &scReqInfo->scBuf))
288 {
289 /* Record the scID and ScBuf in the instance */
290 for (index = 0; index < 4; index++)
291 {
292 if(pChan->scInfo[index].scID == 0)
293 break;
294 }
296 if (index >= 4)
297 {
298 salld_sim_print("SALLD Channel %d: salldsimScAlloc failed due to lack of internal buffer\n!", chnum - 1);
299 salld_sim_free_sc(scReqInfo->scID);
300 scReqInfo->scBuf = (uintptr_t ) NULL;
301 return;
302 }
303 pChan->scInfoIndex = index;
304 pChan->scInfo[index] = *scReqInfo;
305 }
306 else
307 {
308 uint16_t maxAllowed = SALLDSIM_SC_BUF_SIZE;
309 SALog("SALLD Channel %d: salldsimScAlloc failed with size = %d, Max allowed is %d!\n", chnum, scReqInfo->scSize, maxAllowed);
310 scReqInfo->scBuf = (uintptr_t) NULL;
311 }
312 }
314 /**
315 * @brief Callout to externally supplied system to release the security context with the specified ID.
316 * This function must be implemented as a simple non-blocking function.
317 * This is a function pointer and must point to a valid function location who meets the API requirements.
318 *
319 * @param[in] handle SALLD channel instance identifier.
320 * @param[in] scID Security Context ID
321 *
322 */
323 void salldsimScFree (Sa_ChanHandle handle, uint16_t scID)
324 {
325 uint16_t id = Sa_chanGetID(handle);
326 int chnum = SALLDSIM_GET_CHAN(id);
327 #if defined(SAU_PRMOTE_DEMOTE_TEST)
328 salldSimChannel_t *pChan = &tFrameworkSecure.salldSimChn[chnum - 1];
329 #else
330 salldSimChannel_t *pChan = &tFramework.salldSimChn[chnum - 1];
331 #endif
332 int index;
334 /* Record the scID and ScBuf in the instance */
335 for (index = 0; index < 4; index++)
336 {
337 if (pChan->scInfo[index].scID == scID)
338 {
339 pChan->scInfo[index].scID = 0;
340 salld_sim_free_sc(scID);
341 break;
342 }
343 }
345 if (index >= 4)
346 {
347 salld_sim_print("SALLD channel %d: salldsimScFree failed because scID can not be found at channel instance\n!", chnum - 1);
348 }
349 }
351 /**
352 * @brief Callout to externally supplied system to register the security channel with its software
353 * routing information.
354 * This is a function pointer and must point to a valid function location who meets the API requirements.
355 *
356 * @param[in] handle SALLD channel instance identifier.
357 * @param[in] chanSwInfo Pointer to SALLD software routing information structure.
358 *
359 * @sa Sa_SWInfo_t
360 *
361 */
362 void salldsimChanRegister (Sa_ChanHandle handle, Sa_SWInfo_t* pSwInfo)
363 {
364 uint16_t id = Sa_chanGetID(handle);
365 int chnum = SALLDSIM_GET_CHAN(id);
366 #if defined(SAU_PRMOTE_DEMOTE_TEST)
367 salldSimChannel_t *pChan = &tFrameworkSecure.salldSimChn[chnum - 1];
368 #else
369 salldSimChannel_t *pChan = &tFramework.salldSimChn[chnum - 1];
370 #endif
372 pChan->regSwInfo = *pSwInfo;
374 salld_sim_print("SALLD channel %d: Register SA Info\n", chnum - 1);
375 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
376 //salld_sim_output_sainfo(chnum, FALSE, pSwInfo);
378 if ((pChan->state == SALLD_STATE_ACTIVE) && (pChan->protocolType == sa_PT_SRTP))
379 {
380 sauReregegisterUdp(pChan->pConnHdl);
381 }
382 #endif
383 }
385 /**
386 * @brief Callout to externally supplied system to un-register the security channel with its software
387 * routing information.
388 * This is a function pointer and must point to a valid function location who meets the API requirements.
389 *
390 * @param[in] handle SALLD channel instance identifier.
391 * @param[in] chanSwInfo Pointer to SALLD software routing information structure.
392 *
393 * @sa Sa_SWInfo_t
394 *
395 */
396 void salldsimChanUnRegister (Sa_ChanHandle handle, Sa_SWInfo_t* pSwInfo)
397 {
398 uint16_t id = Sa_chanGetID(handle);
399 int chnum = SALLDSIM_GET_CHAN(id);
401 salld_sim_print("SALLD channel %d: UnRegister Sw Info\n", chnum - 1);
402 }
404 /**
405 * @brief Callout to externally supplied system to send an Null packet to the SA sub-system
406 * This is a function pointer and must point to a valid function location who meets the API requirements.
407 *
408 * @param[in] handle SALLD channel instance identifier.
409 * @param[in] pktInfo Pointer to the packet info structure.
410 *
411 * @sa Sa_PktInfo_t
412 *
413 */
414 void salldsimChanSendNullPkt (Sa_ChanHandle handle, Sa_PktInfo_t *pktInfo)
415 {
416 #if defined(SAU_PRMOTE_DEMOTE_TEST)
417 #else
418 uint16_t id = Sa_chanGetID(handle);
419 int chnum = SALLDSIM_GET_CHAN(id);
420 Sa_SWInfo_t* pSwInfo = &pktInfo->swInfo;
422 salld_sim_print("SALLD channel %d: Send Null Packet\n", chnum - 1);
424 testCommonSendNullPkt(&tFramework, pSwInfo->swInfo);
425 #endif
427 }
429 /******************************************************************************/
430 /******************************************************************************
431 ** EXTERNAL API FUNCTIONS (salldSim_...)
432 ******************************************************************************/
433 /******************************************************************************/
435 Sa_ChanHandle sauShadowChanHandle;
437 /*-----------------------------------------------------------------
438 * Function: salldSim_init_chn
439 *
440 * Description: SALLD Channel instantiation.
441 *-----------------------------------------------------------------*/
442 uint16_t salldSim_init_chn (salldSimChannel_t *pSimChan)
443 {
445 uint16_t index = SALLDSIM_GET_CHAN(pSimChan->ID) - 1;
446 Sa_ChanSizeCfg_t sizeCfg;
447 Sa_ChanConfig_t chanCfg;
448 int aligns[sa_CHAN_N_BUFS];
449 uintptr_t bases[sa_CHAN_N_BUFS];
450 int i;
451 int16_t ret_code;
452 Bool fShared = ((pSimChan->protocolType == sa_PT_IPSEC_AH) || (pSimChan->protocolType == sa_PT_IPSEC_ESP))?
453 TRUE:FALSE;
454 char msg_start[80] = "Instantiating SALLD Channel %d ...\n";
456 /* Check that the channel is not already created */
457 if (pSimChan->salldInst != (Sa_ChanHandle)0U) {
458 return (salld_SIM_ERR);
459 }
461 /* Progress (verbose only) */
462 salld_sim_iprint (msg_start, index);
464 /* salldGetSizes() parameters */
465 memset(&sizeCfg, 0, sizeof(Sa_ChanSizeCfg_t));
466 memset(&chanCfg, 0, sizeof(Sa_ChanConfig_t));
467 sizeCfg.protocolType = pSimChan->protocolType;
468 sizeCfg.cacheLineSize = fShared?SYS_CACHE_LINE_SIZE:0;
470 #ifndef NSS_LITE2
471 sizeCfg.ctrlBitMap = sa_SIZE_CONFIG_CREATE_SHADOW_INST;
472 #endif
474 /* Get SALLD Channel buffer requirements and ensure #buffers within limits */
475 ret_code = Sa_chanGetBufferReq (&sizeCfg, pSimChan->buf_sizes, aligns);
476 salld_sim_check_rc (salld_SIM_ERR_SALLDGETBUFDEF, ret_code, sa_ERR_OK);
478 /* Allocate buffers */
479 /* Allocate SALLD memory buffers & increment salld heap size. Data from the (global)
480 variable salldBufs is copied into the (local) variable salldLocalBufs in order to
481 correctly set salldLocalBufs' size parameter */
482 for (i = 0; i < sa_CHAN_N_BUFS; i++)
483 {
484 bases[i] = salld_sim_malloc (pSimChan->buf_sizes[i], aligns[i]);
485 /*
486 * Global address should be used if the channel may be shared among multiple cores
487 */
488 if (fShared)
489 {
490 bases[i] = utilgAddr(bases[i]);
491 }
492 pSimChan->salld_heap_size += pSimChan->buf_sizes[i];
493 }
495 /* Create (NEW) SALLD channel */
496 chanCfg.ID = (salld_MOD_ID << 8) | (index + 1);
497 chanCfg.sizeConfig = sizeCfg;
498 #if defined(SAU_PRMOTE_DEMOTE_TEST)
499 ret_code = Sa_chanCreate (tFrameworkSecure.salld_handle, &chanCfg, (void **) &bases, &pSimChan->salldInst);
500 #else
501 ret_code = Sa_chanCreate (tFramework.salld_handle, &chanCfg, (void **) &bases, &pSimChan->salldInst);
502 #endif
503 salld_sim_check_rc (salld_SIM_ERR_SALLDINIT, ret_code, sa_ERR_OK);
505 /* Progress (verbose only) */
506 salld_SIM_PRINT_DONE ();
508 /* Return success */
509 return (salld_SIM_NOERR);
510 }
512 /*-----------------------------------------------------------------
513 * Function: salldSim_open_chn
514 *
515 * Description: SALLD Channel open.
516 *-----------------------------------------------------------------*/
517 uint16_t salldSim_open_chn (salldSimChannel_t *pChan, salldOpenConfig_t *cfg)
518 {
519 uint16_t index = SALLDSIM_GET_CHAN(pChan->ID) - 1;
520 Sa_ChanCtrlInfo_t chanCtrlInfo;
521 Sa_GenCtrlInfo_t *pGenCtrlInfo = &chanCtrlInfo.ctrlInfo.gen;
522 Sa_KeyCtrlInfo_t *pKeyCtrlInfo = &chanCtrlInfo.ctrlInfo.key;
523 int16_t ret_code;
524 char msg_open[80] = "Opening SALLD Channel %d ...\n";
525 char msg_genCfg[80] = "SALLD Channel %d: General Configuration\n";
526 char msg_keyCfg[80] = "SALLD Channel %d: Key Configuration\n";
527 char msg_txChEnable[80] = "SALLD Channel %d: Enable Tx\n";
528 char msg_rxChEnable[80] = "SALLD Channel %d: Enable Rx\n";
530 /* Ensure channel is available */
531 if (pChan->salldInst == (Sa_ChanHandle)0U)
532 return (salld_SIM_ERR);
534 /* Progress (verbose only) */
535 salld_sim_iprint (msg_open, index);
537 /* Progress (verbose only) */
538 salld_sim_iprint (msg_genCfg, index);
540 memset(pGenCtrlInfo, 0, sizeof(Sa_GenCtrlInfo_t));
541 if (cfg->ctrlBitMap & sa_CONTROLINFO_CTRL_RX_ON)
542 {
543 pGenCtrlInfo->validBitfield = sa_CONTROLINFO_VALID_RX_CTRL |
544 sa_CONTROLINFO_VALID_REPLAY_WIN;
545 pGenCtrlInfo->replayWindowSize = pChan->relayWinSize;
546 pGenCtrlInfo->rxCtrl = pChan->rxInfo.genCfg;
547 }
549 if (cfg->ctrlBitMap & sa_CONTROLINFO_CTRL_TX_ON)
550 {
551 pGenCtrlInfo->validBitfield |= sa_CONTROLINFO_VALID_TX_CTRL;
552 pGenCtrlInfo->txCtrl = pChan->txInfo.genCfg;
553 }
555 chanCtrlInfo.ctrlType = (uint16_t)sa_CHAN_CTRL_GEN_CONFIG;
557 ret_code = Sa_chanControl ((Sa_ChanHandle)pChan->salldInst, &chanCtrlInfo);
558 salld_sim_check_rc (salld_SIM_ERR_SALLDCONTROL, ret_code, sa_ERR_OK);
560 /* Progress (verbose only) */
561 salld_sim_iprint (msg_keyCfg, index);
563 chanCtrlInfo.ctrlType = (uint16_t)sa_CHAN_CTRL_KEY_CONFIG;
564 memset(pKeyCtrlInfo, 0, sizeof(Sa_KeyCtrlInfo_t));
566 if (cfg->ctrlBitMap & sa_CONTROLINFO_CTRL_RX_ON)
567 {
568 pKeyCtrlInfo->ctrlBitfield = sa_KEY_CONTROL_RX_KEY_VALID;
569 pKeyCtrlInfo->rxKey = pChan->rxInfo.keyCfg;
570 }
572 if (cfg->ctrlBitMap & sa_CONTROLINFO_CTRL_TX_ON)
573 {
574 pKeyCtrlInfo->ctrlBitfield |= sa_KEY_CONTROL_TX_KEY_VALID;
575 pKeyCtrlInfo->txKey = pChan->txInfo.keyCfg;
576 }
578 ret_code = Sa_chanControl ((Sa_ChanHandle)pChan->salldInst, &chanCtrlInfo);
579 salld_sim_check_rc (salld_SIM_ERR_SALLDCONTROL, ret_code, sa_ERR_OK);
581 /* Enable Tx and Rx seperately so that we can display the SC accordingly */
582 chanCtrlInfo.ctrlType = (uint16_t)sa_CHAN_CTRL_GEN_CONFIG;
583 memset(pGenCtrlInfo, 0, sizeof(Sa_GenCtrlInfo_t));
584 pGenCtrlInfo->validBitfield = sa_CONTROLINFO_VALID_CTRL_BITMAP;
586 if (cfg->ctrlBitMap & sa_CONTROLINFO_CTRL_TX_ON)
587 {
588 salld_sim_iprint (msg_txChEnable, index);
590 pGenCtrlInfo->ctrlBitfield = cfg->ctrlBitMap & ~sa_CONTROLINFO_CTRL_RX_ON;
592 ret_code = Sa_chanControl ((Sa_ChanHandle)pChan->salldInst, &chanCtrlInfo);
593 salld_sim_check_rc (salld_SIM_ERR_SALLDCONTROL, ret_code, sa_ERR_OK);
595 /* Output the Tx Security Context */
596 //salld_sim_output_sc(index+1, TRUE, &pChan->scInfo[pChan->scInfoIndex]);
598 }
600 if (cfg->ctrlBitMap & sa_CONTROLINFO_CTRL_RX_ON)
601 {
602 #if SA_GEN_TEST_VECTOR_RX
603 Sa_ScReqInfo_t* pReqInfo;
604 #endif
606 salld_sim_iprint (msg_rxChEnable, index);
608 pGenCtrlInfo->ctrlBitfield = cfg->ctrlBitMap;
609 ret_code = Sa_chanControl ((Sa_ChanHandle)pChan->salldInst, &chanCtrlInfo);
610 salld_sim_check_rc (salld_SIM_ERR_SALLDCONTROL, ret_code, sa_ERR_OK);
612 /* Output the Rx Security Context */
613 #if SA_GEN_TEST_VECTOR_RX
614 pReqInfo = &pChan->scInfo[pChan->scInfoIndex];
615 utlOutputSc(&tFramework, pReqInfo->scSize, pReqInfo->scID, pReqInfo->scBuf);
616 #endif
617 }
619 /* Record the new state */
620 pChan->stateBitMap = cfg->ctrlBitMap;
622 pChan->state = SALLD_STATE_ACTIVE;
624 ret_code = Sa_chanGetShadowHandle((Sa_ChanHandle)pChan->salldInst, &sauShadowChanHandle);
626 /* Progress (verbose only) */
627 salld_SIM_PRINT_DONE ();
629 /* Return success */
630 return (salld_SIM_NOERR);
631 }
633 /*-----------------------------------------------------------------
634 * Function: salldSim_close_chn
635 *
636 * Description: SALLD Channel close.
637 *-----------------------------------------------------------------*/
638 uint16_t salldSim_close_chn (salldSimChannel_t *pChan)
639 {
640 int16_t index = SALLDSIM_GET_CHAN(pChan->ID) - 1;
641 int16_t ret_code;
642 void* bases[sa_CHAN_N_BUFS];
643 int i;
644 char msg_chClose[80] = "Closing SALLD Channel %d ...\n";
646 /* Ensure channel is open */
647 if (pChan->salldInst == (Sa_ChanHandle)0U)
648 return (salld_SIM_ERR);
650 /* Progress (verbose only) */
651 salld_sim_iprint (msg_chClose, index);
653 /* Close the SALLD channel */
654 ret_code = Sa_chanClose (pChan->salldInst, bases);
655 salld_sim_check_rc (salld_SIM_ERR_SALLDCLOSE, ret_code, sa_ERR_OK);
657 /* Set Channel state to "closed" */
658 pChan->salldInst = (Sa_ChanHandle)0U;
660 /* Free memory allocated for SALLD buffers & adjust salld_heap_size */
661 for (i = 0; i < sa_CHAN_N_BUFS; i++)
662 {
663 #if !defined(NSS_LITE2)
664 if (((uint32_t)bases[i] & 0xf0000000) == 0x10000000)
665 {
666 /* Convert back to local address */
667 bases[i] = (void *)((uint32_t)bases[i] & 0xffffff);
668 }
669 #endif
671 salld_sim_free (bases[i], pChan->buf_sizes[i]);
672 pChan->salld_heap_size -= pChan->buf_sizes[i];
673 }
675 /* TBD: clear memory */
676 memset(pChan, 0, sizeof(salldSimChannel_t));
677 pChan->ID = (salld_SIM_ID << 8) | (index + 1);
679 pChan->state = SALLD_STATE_IDLE;
681 /* Progress (verbose only) */
682 salld_SIM_PRINT_DONE ();
684 /* Return success */
685 return (salld_SIM_NOERR);
686 }
688 /*-----------------------------------------------------------------
689 * Function: salldSim_poll_chn
690 *
691 * Description: Poll SALLD Channel to simulate some special operations
692 * such as re-key
693 *-----------------------------------------------------------------*/
694 uint16_t salldSim_poll_chn (salldSimChannel_t *pChan)
695 {
696 Sa_ChanCtrlInfo_t chanCtrlInfo;
697 Sa_KeyCtrlInfo_t *pKeyCtrlInfo = &chanCtrlInfo.ctrlInfo.key;
698 Sa_SrtpKeyRequest_t* pKeyReq;
699 Sa_SrtpKeyParams_t* pKeyParams;
700 int16_t ret_code;
702 /* Ensure channel is available */
703 if (pChan->salldInst == (Sa_ChanHandle)0U)
704 return (salld_SIM_ERR);
706 memset(&chanCtrlInfo, 0, sizeof(Sa_ChanCtrlInfo_t));
707 chanCtrlInfo.ctrlType = (uint16_t)sa_CHAN_CTRL_KEY_CONFIG;
709 /* Tx Rekey Operation */
710 if (pChan->stateBitMap & salld_SIM_STATE_TX_REKEY)
711 {
712 pChan->stateBitMap &= ~salld_SIM_STATE_TX_REKEY;
713 pKeyCtrlInfo->ctrlBitfield = sa_KEY_CONTROL_TX_KEY_VALID;
715 pKeyReq = &pChan->txInfo.keyReq.params.srtp;
716 pKeyParams = &pChan->txInfo.keyCfg.srtp;
718 // Clear the roc(index) flag
719 pKeyParams->ctrlBitfield &= ~sa_SRTP_KEY_CTRL_ROC;
721 if (pKeyReq->ctrlBitfield & sa_SRTP_KEY_REQUEST_KEY_TYPE_MKI)
722 {
723 /* MKI key */
724 if (pKeyParams->mkiSize)
725 {
726 pKeyParams->mki++;
727 pKeyParams->keyLifeTimeLsw = 0xffffffff;
729 /* Set the expected RX re-key info */
730 pChan->rxInfo.keyCfg.srtp.mki = pKeyParams->mki;
731 }
732 }
733 else
734 {
735 /* From-To key */
736 /* Update the from To range */
737 pKeyParams->fromEsnMsw = pKeyParams->toEsnMsw;
738 pKeyParams->fromEsnLsw = pKeyParams->toEsnLsw+1;
740 pKeyParams->toEsnLsw += 1000;
741 if(pKeyParams->toEsnLsw < pKeyParams->fromEsnLsw)
742 pKeyParams->toEsnMsw++;
743 }
745 pKeyParams->masterKey[15]++;
746 pKeyParams->masterSalt[13]++;
748 pKeyCtrlInfo->txKey = pChan->txInfo.keyCfg;
750 }
753 if (pChan->stateBitMap & salld_SIM_STATE_RX_REKEY)
754 {
755 pChan->stateBitMap &= ~salld_SIM_STATE_RX_REKEY;
756 pKeyCtrlInfo->ctrlBitfield |= sa_KEY_CONTROL_RX_KEY_VALID;
758 pKeyReq = &pChan->rxInfo.keyReq.params.srtp;
759 pKeyParams = &pChan->rxInfo.keyCfg.srtp;
761 //clear ROC (index) flag
762 pKeyParams->ctrlBitfield &= ~sa_SRTP_KEY_CTRL_ROC;
764 if (pKeyReq->ctrlBitfield & sa_SRTP_KEY_REQUEST_KEY_TYPE_MKI)
765 {
766 if (pKeyReq->ctrlBitfield & sa_SRTP_KEY_REQUEST_MKI_VALID)
767 {
768 if (pKeyParams->mki != pKeyReq->mki)
769 {
770 /* The requested key is not available */
771 pKeyCtrlInfo->ctrlBitfield &= ~sa_KEY_CONTROL_RX_KEY_VALID;
772 }
773 }
774 else
775 {
776 pKeyParams->mki++;
777 }
779 pKeyParams->keyLifeTimeLsw = 0xffffffff;
781 }
782 else
783 {
784 /* From-To key */
785 /* Update the from To range */
786 pKeyParams->fromEsnMsw = pKeyParams->toEsnMsw;
787 pKeyParams->fromEsnLsw = pKeyParams->toEsnLsw+1;
789 pKeyParams->toEsnLsw += 1000;
790 if(pKeyParams->toEsnLsw < pKeyParams->fromEsnLsw)
791 pKeyParams->toEsnMsw++;
792 }
794 /* TBD: It has been updated by TX key already */
795 #if 0
796 pKeyParams->masterKey[15]++;
797 pKeyParams->masterSalt[13]++;
798 #endif
800 pKeyCtrlInfo->rxKey = pChan->rxInfo.keyCfg;
801 }
803 if (pKeyCtrlInfo->ctrlBitfield)
804 {
805 ret_code = Sa_chanControl ((Sa_ChanHandle)pChan->salldInst, &chanCtrlInfo);
806 salld_sim_check_rc (salld_SIM_ERR_SALLDCONTROL, ret_code, sa_ERR_OK);
807 }
809 /* Return success */
810 return (salld_SIM_NOERR);
811 }
814 /*-----------------------------------------------------------------
815 * Function: salldSim_get_stats
816 *
817 * Description: SALLD Channel Satistics.
818 *-----------------------------------------------------------------*/
819 uint16_t salldSim_get_stats (salldSimChannel_t *pChan)
820 {
821 int16_t index = SALLDSIM_GET_CHAN(pChan->ID) - 1;
822 Sa_Stats_t stats;
823 int16_t ret_code = sa_ERR_OK;
824 int i;
825 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
826 volatile uint32_t fakeRead;
827 #endif
828 /* Ensure channel is open */
829 if (pChan->salldInst == (Sa_ChanHandle) 0U)
830 return (salld_SIM_ERR);
832 if (Sa_chanGetStats (pChan->salldInst, sa_STATS_QUERY_FLAG_TRIG, &stats) == sa_ERR_OK)
833 {
834 /* Statistics is already available */
835 salld_sim_disp_chan_stats(index, pChan->protocolType, &stats);
837 /* Return success */
838 return (salld_SIM_NOERR);
839 }
841 /* Wait for the stats reply */
842 for (i = 0; i < 200; i++) {
843 /* Simulator workaround: Kick off the SA simulator clock */
844 /* No effect for the real chip */
845 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
846 fakeRead = *pSAModelTrig;
847 #endif
848 utilCycleDelay (1000);
849 if (Sa_chanGetStats (pChan->salldInst, 0, &stats) == sa_ERR_OK)
850 break;
851 }
853 if (i >= 200)
854 {
855 salld_sim_print("salldSim_get_stats: Stats is not available in time\n");
856 ret_code = Sa_chanGetStats (pChan->salldInst, sa_STATS_QUERY_FLAG_NOW, &stats);
857 }
859 salld_sim_check_rc (salld_SIM_ERR_SALLDSTATS, ret_code, sa_ERR_OK);
861 salld_sim_disp_chan_stats(index, pChan->protocolType, &stats);
863 /* Return success */
864 return (salld_SIM_NOERR);
865 }
867 /*-----------------------------------------------------------------
868 * Function: salldSim_close_all_chns
869 *
870 * Description: SALLD Channel close.
871 *-----------------------------------------------------------------*/
872 void salldSim_close_all_chns (void)
873 {
874 #if defined(SAU_PRMOTE_DEMOTE_TEST)
875 salldSimChannel_t *pChan = &tFrameworkSecure.salldSimChn[0];
876 #else
877 salldSimChannel_t *pChan = &tFramework.salldSimChn[0];
878 #endif
879 int i;
881 for (i = 0; i < numSalldSimChans; i++, pChan++)
882 {
883 salldSim_close_chn(pChan);
884 }
885 numSalldSimChans = 0;
886 }
888 /*-----------------------------------------------------------------
889 * Function: salldSim_get_all_chan_stats
890 *
891 * Description: Inquire all channel stats.
892 *-----------------------------------------------------------------*/
893 void salldSim_get_all_chan_stats (void)
894 {
895 #if defined(SAU_PRMOTE_DEMOTE_TEST)
896 salldSimChannel_t *pChan = &tFrameworkSecure.salldSimChn[0];
897 #else
898 salldSimChannel_t *pChan = &tFramework.salldSimChn[0];
899 #endif
901 int i;
903 for (i = 0; i < numSalldSimChans; i++, pChan++)
904 {
905 salldSim_get_stats(pChan);
906 }
907 }
909 /*-----------------------------------------------------------------
910 * Function: salldSim_get_sys_stats
911 *
912 * Description: SALLD System Satistics.
913 *-----------------------------------------------------------------*/
914 void salldSim_get_sys_stats (void)
915 {
916 Sa_SysStats_t stats;
918 #if defined(SAU_PRMOTE_DEMOTE_TEST)
919 Sa_getSysStats (tFrameworkSecure.salld_handle, &stats);
921 #else
922 Sa_getSysStats (tFramework.salld_handle, &stats);
923 #endif
925 salld_sim_disp_system_stats(&stats);
926 }
928 /******************************************************************************/
929 /******************************************************************************
930 ** LOCAL API FUNCTIONS (salld_sim_...)
931 ******************************************************************************/
932 /******************************************************************************/
934 /*-----------------------------------------------------------------
935 * Function: salld_sim_check_rc
936 *
937 * Description: Check return code for failure.
938 *-----------------------------------------------------------------*/
939 void salld_sim_check_rc (salldSimErr_e id, uint16_t rc, uint16_t pass_code)
940 {
941 char msg_badRc[80] = "\nERROR Bad Return Code: 0x%4x\n\n";
942 /* Wait forever */
943 if (rc != pass_code) {
944 /* Set ID for quick identifcation */
945 salldSimErrorId = id;
946 salld_sim_iprint (msg_badRc, (int32_t) id);
947 salld_sim_halt ();
948 }
949 }
952 /*-----------------------------------------------------------------
953 * Function: salld_sim_print
954 *
955 * Description: Utility to provide static progress information.
956 *-----------------------------------------------------------------*/
957 #define SATEST_PRINT_BUF_LEN (2048U)
958 void salld_sim_print (const char* fmt, ...)
959 {
960 if (simCfg->verbose_print)
961 {
962 #if defined(SOC_AM65XX) || defined (SOC_J721E) || defined (SOC_AM64X)
963 char buf[SATEST_PRINT_BUF_LEN];
964 #endif
965 va_list args;
966 va_start(args, fmt);
967 #ifdef __LINUX_USER_SPACE
968 vprintf(fmt, args);
969 #elif defined(SOC_AM65XX) || defined (SOC_J721E) || defined(SOC_AM64X)
970 SAPrintBuf(buf, SATEST_PRINT_BUF_LEN, (const char *) fmt, args);
971 #else
972 System_vprintf(fmt, args);
973 #endif
974 va_end(args);
976 #if defined(SOC_AM65XX) || defined(SOC_J721E) || defined (SOC_AM64X)
977 SALog(buf);
978 #endif
979 }
980 }
983 /*-----------------------------------------------------------------
984 * Function: salld_sim_iprint
985 *
986 * Description: Utility to provide integer progress information.
987 *-----------------------------------------------------------------*/
988 void salld_sim_iprint (char *str, int32_t ivar)
989 {
990 sprintf (print_string, str, ivar);
991 salld_sim_print (print_string);
992 }
995 /*-----------------------------------------------------------------
996 * Function: salld_sim_sprint
997 *
998 * Description: Utility to provide string progress information.
999 *-----------------------------------------------------------------*/
1000 void salld_sim_sprint (char *str, char *svar)
1001 {
1002 sprintf (print_string, str, svar);
1003 salld_sim_print (print_string);
1004 }
1006 /*-----------------------------------------------------------------
1007 * Function: salld_sim_set_defaults
1008 *
1009 * Description: SALLD simulation default initialization.
1010 *-----------------------------------------------------------------*/
1011 void salld_sim_set_defaults (void)
1012 {
1013 /* Default globals initialization */
1014 salldSimHalt = TRUE;
1015 salldSimErrorId = salld_SIM_ERR_NONE;
1017 /* Default boolean initialization */
1018 simCfg->verbose_print = saTestLogEnable;
1020 }
1022 /*-----------------------------------------------------------------
1023 * Function: salld_sim_disp_control
1024 *
1025 * Description: SALLD print control
1026 * TRUE: force enable
1027 * FALSE: reset to default
1028 *-----------------------------------------------------------------*/
1029 void salld_sim_disp_control (Bool enable)
1030 {
1032 /* Default boolean initialization */
1033 if(enable)
1034 simCfg->verbose_print = TRUE;
1035 else
1036 simCfg->verbose_print = saTestLogEnable;
1038 }
1041 /*-----------------------------------------------------------------
1042 * Function: salld_sim_disp_chan_stats
1043 *
1044 * Description: SALLD simulation display channel statistics.
1045 *-----------------------------------------------------------------*/
1046 void salld_sim_disp_chan_stats(int16_t chanNum, Sa_SecProto_e protocolType, Sa_Stats_t *pStats)
1047 {
1048 switch (protocolType)
1049 {
1050 case sa_PT_NULL:
1051 {
1052 Sa_DataModeStats_t *p_stats = &pStats->data;
1054 salld_sim_print("Chan %d (Data Mode) Statistics:\n", chanNum);
1055 salld_sim_print("----------------------------------------------------\n");
1056 salld_sim_print("numPktsHi = 0x%08x\n", p_stats->pktHi);
1057 salld_sim_print("numPktsLo = 0x%08x\n", p_stats->pktLo);
1058 break;
1059 }
1061 case sa_PT_SRTP:
1062 {
1063 Sa_SrtpStats_t *p_stats = &pStats->srtp;
1065 salld_sim_print("Chan %d (SRTP) Statistics:\n", chanNum);
1066 salld_sim_print("----------------------------------------------------\n");
1067 salld_sim_print("numPktsEnc = 0x%04x%08x\n", p_stats->pktEncHi, p_stats->pktEncLo);
1068 salld_sim_print("numPktsDec = 0x%04x%08x\n", p_stats->pktDecHi, p_stats->pktDecLo);
1069 salld_sim_print("numTxRekey = %d\n", p_stats->txRekey);
1070 salld_sim_print("numRxRekey = %d\n", p_stats->rxRekey);
1071 salld_sim_print("numReplayOld = %d\n", p_stats->replayOld);
1072 salld_sim_print("numReplayDup = %d\n", p_stats->replayDup);
1073 salld_sim_print("numAuthFail = %d\n", p_stats->authFail);
1074 salld_sim_print("txROC = 0x%08x\n", p_stats->txROC);
1075 salld_sim_print("rxROC = 0x%08x\n", p_stats->rxROC);
1076 break;
1077 }
1079 case sa_PT_SRTCP:
1080 {
1081 Sa_SrtcpStats_t *p_stats = &pStats->srtcp;
1083 salld_sim_print("Chan %d (SRTCP) Statistics:\n", chanNum);
1084 salld_sim_print("----------------------------------------------------\n");
1085 salld_sim_print("numPktsEnc = 0x%08x\n", p_stats->pktEnc);
1086 salld_sim_print("numPktsDec = 0x%08x\n", p_stats->pktDec);
1087 salld_sim_print("numTxRekey = %d\n", p_stats->txRekey);
1088 salld_sim_print("numRxRekey = %d\n", p_stats->rxRekey);
1089 salld_sim_print("numReplayOld = %d\n", p_stats->replayOld);
1090 salld_sim_print("numReplayDup = %d\n", p_stats->replayDup);
1091 salld_sim_print("numAuthFail = %d\n", p_stats->authFail);
1092 break;
1093 }
1096 case sa_PT_IPSEC_ESP:
1097 case sa_PT_IPSEC_AH:
1098 {
1099 Sa_IpsecStats_t *p_stats = &pStats->ipsec;
1101 salld_sim_print("Chan %d (IPSEC) Statistics:\n", chanNum);
1102 salld_sim_print("----------------------------------------------------\n");
1103 salld_sim_print("numPktsEncHi = 0x%08x\n", p_stats->pktEncHi);
1104 salld_sim_print("numPktsEncLo = 0x%08x\n", p_stats->pktEncLo);
1105 salld_sim_print("numPktsDecHi = 0x%08x\n", p_stats->pktDecHi);
1106 salld_sim_print("numPktsDecLo = 0x%08x\n", p_stats->pktDecLo);
1107 salld_sim_print("numBytesEncHi = 0x%08x\n", p_stats->txByteCountHi);
1108 salld_sim_print("numBytesEncLo = 0x%08x\n", p_stats->txByteCountLo);
1109 salld_sim_print("numBytesDecHi = 0x%08x\n", p_stats->rxByteCountHi);
1110 salld_sim_print("numBytesDecLo = 0x%08x\n", p_stats->rxByteCountLo);
1111 salld_sim_print("numReplayOld = %d\n", p_stats->replayOld);
1112 salld_sim_print("numReplayDup = %d\n", p_stats->replayDup);
1113 salld_sim_print("numAuthFail = %d\n", p_stats->authFail);
1114 salld_sim_print("txESN = 0x%08x\n", p_stats->txESN);
1115 salld_sim_print("txSN = 0x%08x\n", p_stats->txSN);
1116 salld_sim_print("rxESN = 0x%08x\n", p_stats->rxESN);
1117 break;
1118 }
1120 case sa_PT_3GPP_AC:
1121 {
1123 Sa_AcStats_t *p_stats = &pStats->ac;
1125 salld_sim_print("Chan %d (Air Ciphering) Statistics:\n", chanNum);
1126 salld_sim_print("----------------------------------------------------\n");
1127 salld_sim_print("numPktsToAirHi = 0x%08x\n", p_stats->pktToAirHi);
1128 salld_sim_print("numPktsToAirLo = 0x%08x\n", p_stats->pktToAirLo);
1129 salld_sim_print("numPktsFromAirHi = 0x%08x\n", p_stats->pktFromAirHi);
1130 salld_sim_print("numPktsFromAirLo = 0x%08x\n", p_stats->pktFromAirLo);
1131 salld_sim_print("numAuthFail = %d\n", p_stats->authFail);
1132 salld_sim_print("toAirCount-C = 0x%08x\n", p_stats->toAirCountC);
1133 salld_sim_print("fromAirCount-C = 0x%08x\n", p_stats->fromAirCountC);
1135 break;
1136 }
1138 default:
1139 salld_sim_print("Chan %d Invalid Protocol %d\n", chanNum, protocolType);
1140 break;
1141 }
1142 }
1144 /*-----------------------------------------------------------------
1145 * Function: salld_sim_disp_system_stats
1146 *
1147 * Description: SALLD simulation display system statistics.
1148 *-----------------------------------------------------------------*/
1149 void salld_sim_disp_system_stats(Sa_SysStats_t *p_stats)
1150 {
1151 salld_sim_print("SA System Statistics: Error\n");
1152 salld_sim_print("--------------------------------------------\n");
1153 salld_sim_print("errNoMem = 0x%08x\n", p_stats->err.errNoMem);
1154 salld_sim_print("errCtx = 0x%08x\n", p_stats->err.errCtx);
1155 salld_sim_print("errEngine= 0x%08x\n", p_stats->err.errEngine);
1156 salld_sim_print("errProto = 0x%08x\n", p_stats->err.errProto);
1158 salld_sim_print("\nSA System Statistics: IPSEC ESP\n");
1159 salld_sim_print("--------------------------------------------\n");
1160 salld_sim_print("replayOld = 0x%08x\n", p_stats->esp.replayOld);
1161 salld_sim_print("replayDup = 0x%08x\n", p_stats->esp.replayDup);
1162 salld_sim_print("authFail = 0x%08x\n", p_stats->esp.authFail );
1163 salld_sim_print("pktEncHi = 0x%08x\n", p_stats->esp.pktEncHi );
1164 salld_sim_print("pktEncLo = 0x%08x\n", p_stats->esp.pktEncLo );
1165 salld_sim_print("pktDecHi = 0x%08x\n", p_stats->esp.pktDecHi );
1166 salld_sim_print("pktDecLo = 0x%08x\n", p_stats->esp.pktDecLo );
1168 salld_sim_print("\nSA System Statistics: IPSEC AH\n");
1169 salld_sim_print("--------------------------------------------\n");
1170 salld_sim_print("replayOld = 0x%08x\n", p_stats->ah.replayOld);
1171 salld_sim_print("replayDup = 0x%08x\n", p_stats->ah.replayDup);
1172 salld_sim_print("authFail = 0x%08x\n", p_stats->ah.authFail );
1173 salld_sim_print("pktEncHi = 0x%08x\n", p_stats->ah.pktEncHi );
1174 salld_sim_print("pktEncLo = 0x%08x\n", p_stats->ah.pktEncLo );
1175 salld_sim_print("pktDecHi = 0x%08x\n", p_stats->ah.pktDecHi );
1176 salld_sim_print("pktDecLo = 0x%08x\n", p_stats->ah.pktDecLo );
1178 salld_sim_print("\nSA System Statistics: SRTP\n");
1179 salld_sim_print("--------------------------------------------\n");
1180 salld_sim_print("replayOld = 0x%08x\n", p_stats->srtp.replayOld);
1181 salld_sim_print("replayDup = 0x%08x\n", p_stats->srtp.replayDup);
1182 salld_sim_print("authFail = 0x%08x\n", p_stats->srtp.authFail );
1183 salld_sim_print("pktEncHi = 0x%08x\n", p_stats->srtp.pktEncHi );
1184 salld_sim_print("pktEncLo = 0x%08x\n", p_stats->srtp.pktEncLo );
1185 salld_sim_print("pktDecHi = 0x%08x\n", p_stats->srtp.pktDecHi );
1186 salld_sim_print("pktDecLo = 0x%08x\n", p_stats->srtp.pktDecLo );
1188 salld_sim_print("\nSA System Statistics: Air Ciphering\n");
1189 salld_sim_print("--------------------------------------------\n");
1190 salld_sim_print("authFail = 0x%08x\n", p_stats->ac.authFail );
1191 salld_sim_print("pktToAirHi = 0x%08x\n", p_stats->ac.pktToAirHi );
1192 salld_sim_print("pktToAirLo = 0x%08x\n", p_stats->ac.pktToAirLo );
1193 salld_sim_print("pktFromAirHi= 0x%08x\n", p_stats->ac.pktFromAirHi );
1194 salld_sim_print("pktFromAirLo= 0x%08x\n", p_stats->ac.pktFromAirLo );
1195 }
1198 /*-----------------------------------------------------------------
1199 * Function: salld_download_pdsp_images
1200 *
1201 * Description: Download PDSP images.
1202 *-----------------------------------------------------------------*/
1203 int16_t salld_download_pdsp_images (void)
1204 {
1206 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
1208 uint32_t version;
1209 int i;
1211 Sa_resetControl (tFramework.salld_handle, sa_STATE_RESET);
1213 for ( i = 0; i < nssGblCfgParams.layout.numSaPdsps; i++)
1214 {
1216 Sa_downloadImage (tFramework.salld_handle, i,
1217 (Ptr)nssGblCfgParams.layout.saPdspImage[i],
1218 nssGblCfgParams.layout.saPdspImageSize[i]);
1219 }
1221 if(Sa_resetControl (tFramework.salld_handle, sa_STATE_ENABLE) != sa_STATE_ENABLE)
1222 return (-1);
1224 utilCycleDelay (1000);
1226 for ( i = 0; i < nssGblCfgParams.layout.numSaPdsps; i++)
1227 {
1228 Sa_getPDSPVersion(tFramework.salld_handle, i, &version);
1229 SALog ("PDSP %d version = 0x%08x\n", i, version);
1230 System_flush();
1231 }
1233 #endif
1234 return (sa_ERR_OK);
1235 }
1237 Sa_Handle sauShadowHandle;
1239 /*-----------------------------------------------------------------
1240 * Function: salld_sim_init_sa
1241 *
1242 * Description: Initialize the SA instance.
1243 *-----------------------------------------------------------------*/
1244 void salld_sim_init_sa (uint8_t limitAccess)
1245 {
1246 Sa_SizeCfg_t sizeCfg;
1247 Sa_Config_t cfg;
1248 int aligns[sa_N_BUFS];
1249 void* bases[sa_N_BUFS];
1250 int i;
1251 int16_t ret_code;
1253 /* Progress (verbose only) */
1254 salld_sim_print ("Instantiating SALLD ...");
1256 /* Get SALLD buffer requirements and ensure #buffers within limits */
1257 memset(&sizeCfg, 0, sizeof(Sa_SizeCfg_t));
1258 memset(&cfg, 0, sizeof(Sa_Config_t));
1259 sizeCfg.nMaxChan = salld_SIM_MAX_CHANNELS;
1260 sizeCfg.cacheLineSize = SYS_CACHE_LINE_SIZE;
1261 #ifdef NSS_LITE2
1262 sizeCfg.ctrlBitMap |= sa_SIZE_CONFIG_SASS_UL_GEN2;
1263 #else
1264 sizeCfg.ctrlBitMap = sa_SIZE_CONFIG_CREATE_SHADOW_INST;
1265 if (nssGblCfgParams.layout.fNssGen2)
1266 {
1267 sizeCfg.ctrlBitMap |= sa_SIZE_CONFIG_SASS_GEN2;
1268 }
1269 #endif
1271 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1272 ret_code = Sa_getBufferReq (&sizeCfg, tFrameworkSecure.salld_buf_sizes, aligns);
1273 #else
1274 ret_code = Sa_getBufferReq (&sizeCfg, tFramework.salld_buf_sizes, aligns);
1275 #endif
1276 salld_sim_check_rc (salld_SIM_ERR_SYSGETBUFDEF, ret_code, sa_ERR_OK);
1278 /* Allocate buffers */
1279 /* Allocate SALLD memory buffers & increment salld heap size. Data from the (global)
1280 variable salldBufs is copied into the (local) variable salldLocalBufs in order to
1281 correctly set salldLocalBufs' size parameter */
1282 for (i = 0; i < sa_N_BUFS; i++)
1283 {
1284 /*
1285 * The LLD system buffers may be shared among multiple cores.
1286 * Therefore global address should be provided.
1287 */
1288 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1289 bases[i] = (void *)utilgAddr((uint32_t)salld_sim_malloc (tFrameworkSecure.salld_buf_sizes[i], aligns[i]));
1290 #else
1291 bases[i] = (void *)utilgAddr((uint32_t)salld_sim_malloc (tFramework.salld_buf_sizes[i], aligns[i]));
1292 #endif
1293 }
1295 /* Create SALLD system */
1296 cfg.ID = 0xbabebeef;
1297 cfg.callTable = &salldsim_calloutFunc;
1298 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
1299 cfg.baseAddr = CSL_NETCP_CFG_SA_CFG_REGS;
1300 #else
1301 #ifdef NSS_LITE2
1302 cfg.baseAddr = SA_UTEST_SA2_UL0_BASE;
1303 #else
1304 cfg.baseAddr = CSL_NSS_0_CFG_REGS + 0x00400000;
1305 #endif
1306 #endif
1307 cfg.sizeConfig = &sizeCfg;
1308 cfg.ctrlBitMap = 0;
1310 /* Provide an internal scratch memory for SA */
1311 cfg.intBuf = (void *)gIntBuf;
1312 #ifndef __LINUX_USER_SPACE
1313 cfg.ctrlBitMap = sa_CONFIG_CTRL_BITMAP_TRIGGER_SYS_ERR_HALT;
1314 /*
1315 * Reference only
1316 * This flag can be set only if there are only 3GPP channels running on PDSP2,
1317 * in other word, there are no SRTP channels.
1318 */
1319 // cfg.ctrlBitMap |= sa_CONFIG_CTRL_BITMAP_TRIGGER_PKT_INFO_LOG;
1320 #endif
1322 #if defined(NSS_LITE2) && defined(SAU_PRMOTE_DEMOTE_TEST)
1323 {
1324 uint32_t scPtrPromoteLowRangeL, scPtrPromoteLowRangeH;
1325 uint32_t scPtrPromoteHighRangeL, scPtrPromoteHighRangeH;
1327 scPtrPromoteLowRangeL = ((uint32_t)(((uint64_t)&salldsimScBufSecure[0][0]) >> 0U )) & ((uint32_t) 0xFFFFFFFF);
1328 scPtrPromoteLowRangeH = ((uint32_t)(((uint64_t)&salldsimScBufSecure[0][0]) >> 32U)) & ((uint32_t) 0xFFFFFFFF);
1329 scPtrPromoteHighRangeL = ((uint32_t)(((uint64_t)&salldsimScBufSecure[0][0]) >> 0U)) & ((uint32_t) 0xFFFFFFFF);
1330 scPtrPromoteHighRangeH = ((uint32_t)(((uint64_t)&salldsimScBufSecure[0][0]) >> 32U)) & ((uint32_t) 0xFFFFFFFF);
1332 cfg.scPtrRange.scPtrPromoteLowRangeL = scPtrPromoteLowRangeL & 0x1000;
1333 cfg.scPtrRange.scPtrPromoteLowRangeH = (scPtrPromoteLowRangeH + 0x1000) & 0x1000;
1334 cfg.scPtrRange.scPtrPromoteHighRangeL = scPtrPromoteHighRangeL & 0x1000;
1335 cfg.scPtrRange.scPtrPromoteHighRangeH = (scPtrPromoteHighRangeH + 0x1000) & 0x1000;
1336 cfg.ctrlBitMap |= sa_CONFIG_CTRL_BITMAP_SET_SCPTR_RANGE;
1337 }
1338 #endif
1340 /*
1341 * Has the framework determined if the access should be limited for this
1342 * instance?
1343 */
1344 if (limitAccess)
1345 {
1346 cfg.ctrlBitMap |= sa_CONFIG_CTRL_BITMAP_LIMIT_ACCESS;
1347 }
1349 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1350 ret_code = Sa_create (&cfg, bases, &tFrameworkSecure.salld_handle);
1351 salld_sim_check_rc (salld_SIM_ERR_SYSINIT, ret_code, sa_ERR_OK);
1353 ret_code = Sa_getShadowHandle(tFrameworkSecure.salld_handle, &sauShadowHandle);
1354 #else
1355 ret_code = Sa_create (&cfg, (void **)bases, &tFramework.salld_handle);
1356 salld_sim_check_rc (salld_SIM_ERR_SYSINIT, ret_code, sa_ERR_OK);
1358 ret_code = Sa_getShadowHandle(tFramework.salld_handle, &sauShadowHandle);
1359 #endif
1361 #if !defined(NSS_LITE) && !defined(NSS_LITE2)
1362 /* Download SA PDSP Firmwares */
1363 ret_code = salld_download_pdsp_images();
1365 salld_sim_check_rc (salld_SIM_ERR_PDSPDNLD, ret_code, sa_ERR_OK);
1367 //salld_sim_halt();
1368 #ifdef SA_3GPP_SUPPORT
1369 // Enable 3GPP
1370 Sa_3gppEnabler();
1371 #endif
1372 #endif
1374 /* Progress (verbose only) */
1375 salld_SIM_PRINT_DONE ();
1377 }
1379 /*-----------------------------------------------------------------
1380 * Function: salld_sim_close_sa
1381 *
1382 * Description: SALLD close.
1383 *-----------------------------------------------------------------*/
1384 void salld_sim_close_sa (void)
1385 {
1386 int16_t ret_code;
1387 int i;
1388 void* bases[sa_N_BUFS];
1390 /* Progress (verbose only) */
1391 salld_sim_print ("Closing SALLD ...");
1393 /* Close the SALLD */
1394 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1395 ret_code = Sa_close (tFrameworkSecure.salld_handle, bases);
1396 #else
1397 ret_code = Sa_close (tFramework.salld_handle, bases);
1398 #endif
1399 salld_sim_check_rc (salld_SIM_ERR_SYSCLOSE, ret_code, sa_ERR_OK);
1401 /* Free memory allocated for SALLD buffers & adjust salld_heap_size */
1402 for (i = 0; i < sa_N_BUFS; i++)
1403 {
1404 #if !defined(NSS_LITE2)
1405 if (((uint32_t)bases[i] & 0xf0000000) == 0x10000000)
1406 {
1407 /* Convert back to local address */
1408 bases[i] = (void *)((uint32_t)bases[i] & 0xffffff);
1409 }
1410 #endif
1411 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1412 salld_sim_free (bases[i], tFrameworkSecure.salld_buf_sizes[i]);
1413 #else
1414 salld_sim_free (bases[i], tFramework.salld_buf_sizes[i]);
1415 #endif
1416 }
1418 /* Free memory for SALLD buffer descriptors & adjust aux_heap_size */
1419 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1420 tFrameworkSecure.salld_handle = NULL;
1421 #else
1422 tFramework.salld_handle = (Sa_Handle)0U;
1423 #endif
1424 /* Progress (verbose only) */
1425 salld_SIM_PRINT_DONE ();
1427 }
1429 /*-----------------------------------------------------------------
1430 * Function: salld_sim_initialize
1431 *
1432 * Description: SALLD simulation default initialization.
1433 *-----------------------------------------------------------------*/
1434 void salld_sim_initialize (uint8_t limitAccess)
1435 {
1436 int16_t i;
1438 salld_sim_set_defaults();
1440 #if defined(SAU_PRMOTE_DEMOTE_TEST)
1441 /* Initialize SALLD engine and channel structures */
1442 for (i=0; i<salld_SIM_MAX_CHANNELS; i++) {
1443 memset(&tFrameworkSecure.salldSimChn[i], 0, sizeof(salldSimChannel_t));
1444 tFrameworkSecure.salldSimChn[i].ID = (salld_SIM_ID << 8) | (i + 1);
1445 tFrameworkSecure.salldSimChn[i].state = SALLD_STATE_IDLE;
1446 tFrameworkSecure.salldSimChn[i].rxInfo.pgnIndex = SALLD_PGN_INDEX_NONE;
1447 tFrameworkSecure.salldSimChn[i].txInfo.pgnIndex = SALLD_PGN_INDEX_NONE;
1448 }
1449 #else
1450 /* Initialize SALLD engine and channel structures */
1451 for (i=0; i<salld_SIM_MAX_CHANNELS; i++) {
1452 memset(&tFramework.salldSimChn[i], 0, sizeof(salldSimChannel_t));
1453 tFramework.salldSimChn[i].ID = (salld_SIM_ID << 8) | (i + 1);
1454 tFramework.salldSimChn[i].state = SALLD_STATE_IDLE;
1455 tFramework.salldSimChn[i].rxInfo.pgnIndex = SALLD_PGN_INDEX_NONE;
1456 tFramework.salldSimChn[i].txInfo.pgnIndex = SALLD_PGN_INDEX_NONE;
1457 }
1458 #endif
1460 /* Initialize the Security context management system */
1461 salld_sim_init_sc();
1463 /* Initialize the SA system */
1464 salld_sim_init_sa(limitAccess);
1466 }
1468 /* Nothing past this point */