This commit adds the following:
[keystone-rtos/netapi.git] / ti / runtime / netapi / src / netapi_init.c
1 /****************************************************************************
2 *  FILE:  netapi_init.c
3 *  Global, Private initialization and cleanup routines and defines of NETAPI
4  *
5  * DESCRIPTION: Functions to initialize and cleanup framework resources 
6  *              for running NETAPI
7  *
8  * REVISION HISTORY:
9  *
10  *  Copyright (c) Texas Instruments Incorporated 2013
11  * 
12  *  Redistribution and use in source and binary forms, with or without 
13  *  modification, are permitted provided that the following conditions 
14  *  are met:
15  *
16  *    Redistributions of source code must retain the above copyright 
17  *    notice, this list of conditions and the following disclaimer.
18  *
19  *    Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the 
21  *    documentation and/or other materials provided with the   
22  *    distribution.
23  *
24  *    Neither the name of Texas Instruments Incorporated nor the names of
25  *    its contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
29  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
30  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
32  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
33  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
34  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
37  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
38  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  *
40  ***************************************************************************/
42 #include "netapi.h"
43 #include "netapi_loc.h"
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <sys/mman.h>
48 #include <errno.h>
49 #include <unistd.h>
51 #include "ti/drv/nwal/nwal.h"
53 /* CSL RL includes */
54 #include <ti/csl/cslr_device.h>
55 #include <ti/csl/cslr_qm_config.h>
56 #include <ti/csl/cslr_qm_descriptor_region_config.h>
57 #include <ti/csl/cslr_qm_queue_management.h>
58 #include <ti/csl/cslr_qm_queue_status_config.h>
59 #include <ti/csl/cslr_qm_intd.h>
60 #include <ti/csl/cslr_pdsp.h>
61 #include <ti/csl/csl_qm_queue.h>
62 #include <ti/csl/cslr_cppidma_global_config.h>
63 #include <ti/csl/cslr_cppidma_rx_channel_config.h>
64 #include <ti/csl/cslr_cppidma_rx_flow_config.h>
65 #include <ti/csl/cslr_cppidma_tx_channel_config.h>
66 #include <ti/csl/cslr_cppidma_tx_scheduler_config.h>
67 #include <ti/csl/csl_cppi.h>
68 #include <ti/csl/csl_pscAux.h>
69 #include <ti/csl/csl_semAux.h>
70 #include <ti/csl/csl_cacheAux.h>
71 #include <ti/csl/csl_xmcAux.h>
72 #include <ti/drv/qmss/qmss_qm.h>
74 extern NETAPI_SHM_T* pnetapiShm;
75 /* Global variablesto hold virtual address of various subsystems */
76 extern hplib_virtualAddrInfo_T netapi_VM_VirtAddr[];
78 /* Global variables which needs to be populated with memory pool attributes
79    which is passed to HPLIB for memory pool initialization*/
80 extern hplib_memPoolAttr_T netapi_VM_MempoolAttr[];
81 extern unsigned char *netapi_VM_QMemLocalDescRam;
82 extern unsigned char *netapi_VM_QMemGlobalDescRam;
83 extern unsigned char *netapi_VM_SaContextVaddr;
85 extern Pktlib_HeapIfTable  netapi_pktlib_ifTable;
86 extern NETAPI_GLOBAL_T netapi_global;
89 /* TODO verify: */
90 #define CACHE_LINESZ    64
93 #define ALIGN(x)    __attribute__((aligned (x)))
95 /*****************************************************************************
96  * Global Resources shared by all Cores
97  *****************************************************************************/
98 uint8_t *QMemGlobDescRam = 0;
99 uint8_t *cppiMemPaSaLinkBuf = 0;
100 uint8_t *cppiMemSaPaLinkBuf = 0;
102 /*****************************************************************************
103  * Local Resource allocated at each Core
104  *****************************************************************************/
105 /* Descriptors in global shared */
106 uint8_t *QMemLocDescRam = NULL;
107 uint8_t *cppiMemRxPktLinkBuf = NULL;
108 uint8_t *cppiMemTxPktLinkBuf = NULL;
109 uint8_t *cppiMemRxCtlLinkBuf = NULL;
110 uint8_t *cppiMemTxCtlLinkBuf = NULL;
113 /********************************************************************
114  * FUNCTION PURPOSE:  Internal NETAPI function to setup the QM memory region
115  ********************************************************************
116  * DESCRIPTION:  Internal NETAPI function to setup the QM memory region,
117  *               once per SOC
118  ********************************************************************/
119 int netapip_qmSetupMemRegion(
120                       uint32_t          numDesc,
121                       uint32_t          descSize,
122                       uint32_t*         pDescMemBase,
123                       Qmss_MemRegion    memRegion)
125     Qmss_MemRegInfo   memInfo;
126     Int32             result;
127     Int               n;
128     static int  netapi_qm_region_index=0;
130     memset(&memInfo,0,sizeof(Qmss_MemRegInfo));
131     memInfo.descBase       = pDescMemBase;
132     memInfo.descSize       = descSize;
133     memInfo.descNum        = numDesc;
134     memInfo.manageDescFlag = Qmss_ManageDesc_MANAGE_DESCRIPTOR;
135     memInfo.memRegion      = memRegion;
137     if(memRegion == TUNE_NETAPI_QM_GLOBAL_REGION)
138     {
139         memInfo.startIndex = TUNE_NETAPI_QM_START_INDEX;  //was 0
140         netapi_qm_region_index += numDesc;
141     }else if(memRegion ==NETAPI_LOCAL_REGION)
142     {
143         /* 2nd region for descriptors (perhaps private?) */
144         memInfo.startIndex     = netapi_qm_region_index;
145     }
146     else
147     {
148         return -1 ;
149     }
151     memset (pDescMemBase, 0, (descSize * numDesc));
153     result = Qmss_insertMemoryRegion (&memInfo);
154     if (result < QMSS_SOK)  
155     {
156         netapi_Log ("netapip_qmSetupMemRegion: Qmss_insertMemoryRegion returned error code %d\n", result);
157         return (-1);
158     }
160     return 1;
163 /********************************************************************
164  * FUNCTION PURPOSE:  Internal NETAPI function to start QM
165  ********************************************************************
166  * DESCRIPTION:  Internal NETAPI function to start QM
167  *               once per thread/core
168  ********************************************************************/
169 int netapip_startQm(void)
171      int32_t          result;
172      result = Qmss_start();
173      if (result != QMSS_SOK)
174      {
175          netapi_Log ("netapip_startQm: Qmss_start failed with error code %d\n", result);
176          return (-1);
177      }
178      return 1;
182 /*** NWAL Memory Buffer Configuration ***/
183 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE     3400
184 uint8_t nwalInstMem[NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE]ALIGN(CACHE_LINESZ);
186 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_MAC                    128
187 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_IPSEC_HANDLE_PER_CHAN      256
188 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_IP                     128
189 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_PORT                   128
190 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_L2L3_HDR               128
191 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_LOC_CONTEXT            384
192 #define NWAL_CHAN_HANDLE_SIZE    ((NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_MAC * TUNE_NETAPI_MAX_NUM_MAC) + \
193                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_IPSEC_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2) + \
194                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_IP * TUNE_NETAPI_MAX_NUM_IP) + \
195                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_PORT * TUNE_NETAPI_MAX_NUM_PORTS)+ \
196                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_LOC_CONTEXT * TUNE_NETAPI_NUM_CORES) + \
197                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_L2L3_HDR * TUNE_NETAPI_MAX_NUM_L2_L3_HDRS))
199 uint8_t nwalHandleMem[NWAL_CHAN_HANDLE_SIZE]ALIGN(CACHE_LINESZ);
202 typedef struct NETAPI_PA_SHM_Tag
204 /* todo: Check if below size information can be made available from pa interface file */
205 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0      128
206 /* PA instance */
207 /* Memory used for the PA Instance. Needs to be assigned global uncached memory for chip */
208 uint8_t paBuf0[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0]ALIGN(CACHE_LINESZ);
210 /* Memory used for PA handles */
211 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1    128 * TUNE_NETAPI_MAX_NUM_MAC
212 uint8_t paBuf1[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1]ALIGN(CACHE_LINESZ);
214 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2   13824
217 uint8_t paBuf2[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2]ALIGN(CACHE_LINESZ);
218 } NETAPI_PA_SHM_T;
221 typedef struct NETAPI_SA_SHM_Tag
223 /* Memory used for SA LLD global Handle */
224 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE    512
225 uint8_t salldHandle[NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE]ALIGN(CACHE_LINESZ);
227 /* Memory used by SA LLD per Channel */
228 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN          512
229 uint8_t salldChanHandle[NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2]ALIGN(CACHE_LINESZ);
230 } NETAPI_SA_SHM_T;
232 /********************************************************************
233 * FUNCTION PURPOSE:  NETAPI internal function to gracefully cleanup when startup
234 *                                  issue occurs.
235  ********************************************************************
236  * DESCRIPTION:  NETAPI internal function to gracefully cleanup when startup
237 *                                  issue occurs.
238  ********************************************************************/
239 void netapipErrTeardown() { netapip_cleanupAtStart(); exit(-99); }
241 /********************************************************************
242  * FUNCTION PURPOSE:  Internal NETAPI function to initialize NWAL subsystem
243  ********************************************************************
244  * DESCRIPTION:  Internal NETAPI function to initialize NWAL subsytem
245  ********************************************************************/
246 int netapip_initNwal(
247     int region2use,
248     Pktlib_HeapIfTable * p_table,
249     NETAPI_NWAL_GLOBAL_CONTEXT_T * p_nwal_context,
250     NETAPI_CFG_T*p_cfg )
252     nwalSizeInfo_t  nwalSizeInfo;
253     nwal_RetValue   nwalRetVal;
254     nwalGlobCfg_t   nwalGlobCfg;
255     uint8_t         count;
256     int             sizes[nwal_N_BUFS];
257     int             aligns[nwal_N_BUFS];
258     void*           bases[nwal_N_BUFS];   
259     Pktlib_HeapCfg      heapCfg;
260     int32_t             errCode;
261     void* pBase = NULL;
262     NETAPI_PA_SHM_T* paEntry = NULL;
263     NETAPI_SA_SHM_T* saEntry = NULL;
265     memset(p_nwal_context,0,sizeof( NETAPI_NWAL_GLOBAL_CONTEXT_T) );
266     memset(&nwalGlobCfg,0,sizeof(nwalGlobCfg_t ) );
268     pBase = hplib_shmOpen();
269     if (pBase)
270     {
271         if(hplib_shmAddEntry(pBase, sizeof(NETAPI_PA_SHM_T), PA_ENTRY) == hplib_OK)
272         {
273             paEntry = (NETAPI_PA_SHM_T*)hplib_shmGetEntry(pBase,PA_ENTRY);
274             nwalGlobCfg.instPoolBaseAddr = (void *)paEntry;
275             printf("netapip_initNwal: instPoolBaseAddr: 0x%x\n", nwalGlobCfg.instPoolBaseAddr);
276         }
277         else
278         {
279             netapi_Log("netapip_initNwal: Unable to Add shared memory segment for PASS\n");
280             return -1;
281         }
282         if(hplib_shmAddEntry(pBase, sizeof(NETAPI_SA_SHM_T), SA_ENTRY) == hplib_OK)
283         {
284             saEntry = (NETAPI_PA_SHM_T*)hplib_shmGetEntry(pBase,SA_ENTRY);
285             nwalGlobCfg.instPoolSaBaseAddr = (void*) saEntry;
286         }
287         else
288         {
289             netapi_Log("netapip_initNwal: Unable to Add shared memory segment for SASS\n");
290             return -1;
291         }
292         
293     }
294     /* Initialize Buffer Pool for NetCP PA to SA packets */
295     nwalGlobCfg.pa2SaBufPool.numBufPools = 1;
296     nwalGlobCfg.pa2SaBufPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
297     nwalGlobCfg.pa2SaBufPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
299     /* Initialize the heap configuration. */
300     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
301     /* Populate the heap configuration */
302     heapCfg.name                = "nwal PA2SA";
303     heapCfg.memRegion           = region2use;
304     heapCfg.sharedHeap          = 0;
305     heapCfg.useStarvationQueue  = 0;
306     heapCfg.dataBufferSize      = p_cfg->def_heap_buf_size;
307     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_MAX_PA_TO_SA_DESC;
308     heapCfg.numZeroBufferPackets= 0;
309     heapCfg.heapInterfaceTable.data_malloc  = p_table->data_malloc;
310     heapCfg.heapInterfaceTable.data_free    = p_table->data_free;
311     heapCfg.dataBufferPktThreshold   = 0;
312     heapCfg.zeroBufferPktThreshold   = 0;
315     nwalGlobCfg.pa2SaBufPool.bufPool[0].heapHandle = p_nwal_context->pa2sa_heap=
316                       Pktlib_createHeap(&heapCfg, &errCode);
317     if(nwalGlobCfg.pa2SaBufPool.bufPool[0].heapHandle == NULL)
318     {
319         netapi_Log ("netapip_initNwal: Pktlib_createHeap:Heap Creation Failed for PA to SA Buffer Pool, Error Code: %d\n",
320                     errCode); 
321         netapipErrTeardown();
322         return -1;
323     }
325  /* Initialize Buffer Pool for NetCP SA to PA packets */
326     nwalGlobCfg.sa2PaBufPool.numBufPools = 1;
327     nwalGlobCfg.sa2PaBufPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
328     nwalGlobCfg.sa2PaBufPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
330      /* Populate the heap configuration */
331     heapCfg.name                = "nwal SA2PA";
332     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_MAX_SA_TO_PA_DESC;
334     nwalGlobCfg.sa2PaBufPool.bufPool[0].heapHandle = p_nwal_context->sa2pa_heap=
335         Pktlib_createHeap(&heapCfg, &errCode);
336     if(nwalGlobCfg.sa2PaBufPool.bufPool[0].heapHandle == NULL)
337     {
338         netapi_Log ("netapip_initNwal: Pktlib_createHeap:Heap Creation Failed for SA to PA Buffer Pool, Error Code: %d\n",
339                      errCode); 
340         netapipErrTeardown();
341         return -1;
342     }
344     nwalGlobCfg.hopLimit = 5;/* Default TTL / Hop Limit */
345     nwalGlobCfg.paPowerOn = nwal_TRUE;
346     nwalGlobCfg.saPowerOn = nwal_TRUE;
347     nwalGlobCfg.paFwActive = nwal_TRUE;
348     nwalGlobCfg.saFwActive = nwal_FALSE;
350     /* Pick Default Physical Address */
351     nwalGlobCfg.paVirtBaseAddr = (uint32_t) netapi_VM_VirtAddr->passCfgVaddr;
352     nwalGlobCfg.saVirtBaseAddr = (uint32_t) netapi_VM_VirtAddr->passCfgVaddr +
353                                  ((uint32_t)CSL_PA_SS_CFG_CP_ACE_CFG_REGS - (uint32_t)CSL_PA_SS_CFG_REGS) ;
354     nwalGlobCfg.rxDefPktQ = QMSS_PARAM_NOT_SPECIFIED;
356     /* Get the Buffer Requirement from NWAL */
357     memset(&nwalSizeInfo,0,sizeof(nwalSizeInfo));
358     nwalSizeInfo.nMaxMacAddress = TUNE_NETAPI_MAX_NUM_MAC;
359     nwalSizeInfo.nMaxIpAddress = TUNE_NETAPI_MAX_NUM_IP;
360     nwalSizeInfo.nMaxL4Ports = TUNE_NETAPI_MAX_NUM_PORTS;
361     nwalSizeInfo.nMaxIpSecChannels = TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS;//we allocate 2 per channel
362     nwalSizeInfo.nMaxDmSecChannels = TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS;//we allocate 2 per channel
363     nwalSizeInfo.nMaxL2L3Hdr = TUNE_NETAPI_MAX_NUM_L2_L3_HDRS;
364     nwalSizeInfo.nProc = TUNE_NETAPI_NUM_CORES;
365     nwalRetVal = nwal_getBufferReq(&nwalSizeInfo,
366                                    sizes,
367                                    aligns);
368     if(nwalRetVal != nwal_OK)
369     {
370         netapi_Log ("netapip_initNwal: nwal_getBufferReq Failed %d\n", nwalRetVal);
371         return nwal_FALSE;
372     }
374 /* Check for memory size requirement and update the base */
375     count = 0;
376     bases[nwal_BUF_INDEX_INST] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)nwalInstMem);
377     if(NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE < sizes[nwal_BUF_INDEX_INST])
378     {
379         /* Resize Memory */
380         while(1);
381     }
382     count++;
384     bases[nwal_BUF_INDEX_INT_HANDLES] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)nwalHandleMem);
385     if(NWAL_CHAN_HANDLE_SIZE  < sizes[nwal_BUF_INDEX_INT_HANDLES])
386     {
387         /* Resize Memory */
388         while(1);
389     }
390     count++;
391      bases[nwal_BUF_INDEX_PA_LLD_BUF0] =
392         (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paEntry->paBuf0);
393     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0) < sizes[nwal_BUF_INDEX_PA_LLD_BUF0])
394     {
395         /* Resize Memory */
396         while(1);
397     }
398     count++;
400     bases[nwal_BUF_INDEX_PA_LLD_BUF1] =
401         (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paEntry->paBuf1);
402     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1) < sizes[nwal_BUF_INDEX_PA_LLD_BUF1])
403     {
404         /* Resize Memory */
405         while(1);
406     }
407     count++;
409     bases[nwal_BUF_INDEX_PA_LLD_BUF2] =
410         (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paEntry->paBuf2);
411     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2) < sizes[nwal_BUF_INDEX_PA_LLD_BUF2])
412     {
413         /* Resize Memory */
414         while(1);
415     }
416     count++;
417 #ifdef NETAPI_ENABLE_SECURITY
418     bases[nwal_BUF_INDEX_SA_LLD_HANDLE] =
419         (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)saEntry->salldHandle);
420     if((NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE) < sizes[nwal_BUF_INDEX_SA_LLD_HANDLE])
421     {
422         /* Resize Memory */
423         while(1);
424     }
425     count++;
427     bases[nwal_BUF_INDEX_SA_CONTEXT] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)netapi_VM_SaContextVaddr);
428     /* also save this here for easy access to sa_start */
429     nwalGlobCfg.scPoolBaseAddr = bases[nwal_BUF_INDEX_SA_CONTEXT];
430     count++;
432     bases[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] =
433         (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)saEntry->salldChanHandle);
434     if((NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2) <
435         sizes[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE])
436     {
437         /* Resize Memory */
438         while(1);
439     }
440     count++;
441 #else
442     bases[nwal_BUF_INDEX_SA_LLD_HANDLE] = 0;
443     bases[nwal_BUF_INDEX_SA_CONTEXT] = 0;
444     bases[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] = 0;
445     count = count+3;
446 #endif
447     if(count != nwal_N_BUFS)
448     {
449         while(1);
450     }
452     /* Initialize NWAL module */
453     nwalRetVal = nwal_create(&nwalGlobCfg,
454                              &nwalSizeInfo,
455                              sizes,
456                              bases,
457                              &p_nwal_context->nwalInstHandle);
458     if(nwalRetVal != nwal_OK)
459     {
460         netapi_Log ("netapip_initNwal: nwal_create Failed %d\n",nwalRetVal);
461         while(1);
462     }
464     netapi_Log("netapip_initNwal- Global and Local Network initialization Successful \n");
465     return 1;
468 /********************************************************************
469  * FUNCTION PURPOSE:  Internal NETAPI function to start  NWAL 
470  ********************************************************************
471  * DESCRIPTION:  Internal NETAPI function to start NWAL, per thread/core
472  ********************************************************************/
473 int netapip_startNwal(Pktlib_HeapHandle pkt_heap,
474                       Pktlib_HeapHandle cmd_rx_heap,
475                       Pktlib_HeapHandle cmd_tx_heap,
476                       NETAPI_NWAL_LOCAL_CONTEXT_T *p,
477                       NETAPI_CFG_T *p_cfg,
478                       NETAPI_NWAL_GLOBAL_CONTEXT_T * p_nwal_glob_context  )
480     nwalLocCfg_t    nwalLocCfg;
481     int count=0;
482     static int first_time = 0;
483     nwal_RetValue       nwalRetVal;
485     memset(&nwalLocCfg,0,sizeof(nwalLocCfg));
487     /* Update the Start of Packet Offset for the default flows created 
488      * by NWAL
489      */
490     nwalLocCfg.rxSopPktOffset = p_cfg->def_flow_pkt_rx_offset;
491     nwalLocCfg.rxPktTailRoomSz = p_cfg->def_heap_tailroom_size;
493  /* Call back registration for the core */
494     nwalLocCfg.pRxPktCallBack = netapip_pktioNWALRxPktCallback;
495     nwalLocCfg.pCmdCallBack = netapip_netcpCfgNWALCmdCallBack;
496     nwalLocCfg.pPaStatsCallBack = netapip_netcpCfgNWALCmdPaStatsReply;
498     nwalLocCfg.pRxDmCallBack=  netapip_pktioNWALSBPktCallback; //sideband mode callback
500     /* Initialize Buffer Pool for Control packets from NetCP to Host */
501     nwalLocCfg.rxCtlPool.numBufPools = 1;
502     nwalLocCfg.rxCtlPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
503     nwalLocCfg.rxCtlPool.bufPool[0].bufSize = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
504     nwalLocCfg.rxCtlPool.bufPool[0].heapHandle = cmd_rx_heap;
506     /* Initialize Buffer Pool for Control packets from Host to NetCP */
507     nwalLocCfg.txCtlPool.numBufPools = 1;
508     nwalLocCfg.txCtlPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
509     nwalLocCfg.txCtlPool.bufPool[0].bufSize = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
510     nwalLocCfg.txCtlPool.bufPool[0].heapHandle =  cmd_tx_heap;
512 /* Initialize Buffer Pool for Packets from NetCP to Host */
513     nwalLocCfg.rxPktPool.numBufPools = 1;
514     nwalLocCfg.rxPktPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
515     nwalLocCfg.rxPktPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
516     nwalLocCfg.rxPktPool.bufPool[0].heapHandle = pkt_heap;
518 /* Initialize Buffer Pool for Packets from Host to NetCP */
519     nwalLocCfg.txPktPool.numBufPools = 1;
520     nwalLocCfg.txPktPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
521     nwalLocCfg.txPktPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
522     nwalLocCfg.txPktPool.bufPool[0].heapHandle = pkt_heap;
524     memcpy(&p->nwalLocCfg,&nwalLocCfg,sizeof(nwalLocCfg_t));
525     while(1)
526     {
527         nwalRetVal = nwal_start(p_nwal_glob_context->nwalInstHandle,&nwalLocCfg);
528         if(nwalRetVal == nwal_ERR_INVALID_STATE)
529         {
530             continue;
531         }
532         break;
533     }
535     if(nwalRetVal != nwal_OK)
536     {
537         netapi_Log (">nwal_start:Failed ->err %d !!!\n", nwalRetVal);
538         return -1;
539     }
540     p->state = NETAPI_NW_CXT_LOC_ACTIVE;
541     return 1;
545 /***********************************************************************
546  * FUNCTION PURPOSE:  Internal NETAPI function to initialize the 64 bit timer.
547  ***********************************************************************
548  * DESCRIPTION:  Internal NETAPI function to initialize the 64 bit timer. for tci6614 ONLY
549  **********************************************************************/
550 int netapip_initTimer(void)
552 #ifdef CORTEX_A8
553         return t64_start();
554 #endif
560 /***************************************************************************
561 * FUNCTION PURPOSE:  NETAPI internal function for virtual memory allocation.
562  ***************************************************************************
563  * DESCRIPTION:  NETAPI internal function for virtual memory allocation.
564  **************************************************************************/
566 static uint8_t* netapip_sharedMemoryMalloc(uint32_t size)
568 return  (uint8_t *)hplib_vmMemAlloc(size +netapi_global.cfg.def_heap_extra_size , 128, 0); 
571 /********************************************************************
572 * FUNCTION PURPOSE:  NETAPI internal function for virtual memory free.
573  ********************************************************************
574  * DESCRIPTION:  NETAPI internal function for virtual memory free.
575  ********************************************************************/
576 static void netapip_sharedMemoryFree(uint8_t* ptr, uint32_t size)
578     /* Do Nothing. */
579     netapi_Log("netapi Unexpected.  need to provide a free () for some reason!! \n");
580     return;
583 //defensive: clean out stuff hanging around
584 //
585 //  open a bunch of free queues and zap them
586 #define NQUEUES2CLEAR 35
587 static Qmss_QueueHnd tempQH[NQUEUES2CLEAR];
588 void netapip_cleanupAtStart(void)
590     int i;
591     uint8_t         isAllocated;
593     for(i=0;i<NQUEUES2CLEAR;i++) 
594     {
595         tempQH[i] = Qmss_queueOpen(Qmss_QueueType_GENERAL_PURPOSE_QUEUE,
596                                                   QMSS_PARAM_NOT_SPECIFIED, &isAllocated);
597         netapip_zapQ(tempQH[i]);
598     }
600     for(i=0;i<NQUEUES2CLEAR;i++)
601     {
602         Qmss_queueClose(tempQH[i]);
603     }
608 /********************************************************************
609 * FUNCTION PURPOSE:  NETAPI internal function system initialization
610  ********************************************************************
611  * DESCRIPTION:  NETAPI internal function system initialization
612  ********************************************************************/
613 int netapip_systemInit(NETAPI_HANDLE_T * handle, Bool global_master_process)
615     int32_t             result;
616     Pktlib_HeapHandle   sharedHeapHandle;
617     Pktlib_HeapHandle   controlRxHeapHandle,controlTxHeapHandle;    
618     Pktlib_HeapCfg      heapCfg;
619     int32_t             errCode;
620     int count=0;
622 #ifdef NETAPI_USE_DDR
623     /* Init attributes for DDR */
624     netapi_VM_MempoolAttr[0].attr = HPLIB_ATTR_KM_CACHED0;
625     netapi_VM_MempoolAttr[0].phys_addr = 0;
626     netapi_VM_MempoolAttr[0].size = 0;
628        /* Init attributes for un-cached MSMC */
629     netapi_VM_MempoolAttr[1].attr = HPLIB_ATTR_UN_CACHED;
630     netapi_VM_MempoolAttr[1].phys_addr = CSL_MSMC_SRAM_REGS;
631     netapi_VM_MempoolAttr[1].size = TUNE_NETAPI_PERM_MEM_SZ;
632 #else
633     netapi_VM_MempoolAttr[1].attr = HPLIB_ATTR_KM_CACHED0;
634     netapi_VM_MempoolAttr[1].phys_addr = 0;
635     netapi_VM_MempoolAttr[1].size = 0;
637        /* Init attributes for un-cached MSMC */
638     netapi_VM_MempoolAttr[0].attr = HPLIB_ATTR_UN_CACHED;
639     netapi_VM_MempoolAttr[0].phys_addr = CSL_MSMC_SRAM_REGS;
640     netapi_VM_MempoolAttr[0].size = TUNE_NETAPI_PERM_MEM_SZ;
641 #endif
642     /* initialize all the memory we are going to use
643        - chunk for buffers, descriptors
644        - memory mapped peripherals we use, such as QMSS, PA, etc */
645     result = hplib_vmInit(&netapi_VM_VirtAddr[0], 2, &netapi_VM_MempoolAttr[0]);
647     if (global_master_process == TRUE)
648     {
649         hplibp_initMallocArea(0);
650         hplibp_initMallocArea(1);
651     }
652     else
653     {
654         if (hplibp_checkMallocArea(0) != hplib_OK)
655             return -1;
656         if (hplibp_checkMallocArea(1) != hplib_OK)
657             return -1;
658             }
660     if (result == hplib_OK) netapi_Log("netapip_systemInit: - memory set  up OK\n");
661     else {netapi_Log("netapip_systemInit: - memory set up failed\n"); return -1;}
664 #ifdef NETAPI_ENABLE_SECURITY
665 #define SEC_CONTEXT_SZ 384  //not tunable
666 /* allocate 2x number of tunnels since we need one for inflow and one for data mode */
667     netapi_VM_SaContextVaddr = hplib_vmMemAlloc((TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2 *
668                                         SEC_CONTEXT_SZ), 128, 0);
669     if (!netapi_VM_SaContextVaddr)
670     {
671         netapi_Log("netapip_systemInit: Failed to map SA context memory region\n");
672         return (-1);
673     }
674     netapi_Log("netapip_systemInit: SA Memory mapped/allocated at address %p.\n", netapi_VM_SaContextVaddr);
676 #else
677    netapi_VM_SaContextVaddr= (char *) NULL;
678 #endif
680 /*  TODO: the QM regions is application specific and needs to be moved 
681     since number of regions created is appliction specific, put this in netapip_systemInit */
682     /* (3) Allocate 2 QM regions from continguous chunk above */
683     netapi_VM_QMemGlobalDescRam = 
684                                   (void *)hplib_vmMemAlloc((TUNE_NETAPI_NUM_GLOBAL_DESC *
685                                                             TUNE_NETAPI_DESC_SIZE),
686                                                              128, 0);
688     netapi_VM_QMemLocalDescRam =
689                                   (void *)hplib_vmMemAlloc((TUNE_NETAPI_NUM_LOCAL_DESC *
690                                                             TUNE_NETAPI_DESC_SIZE),
691                                                             128, 0);
692     netapi_Log("netapip_systemInit: desc region=%x global desc region=%x\n", netapi_VM_QMemLocalDescRam, netapi_VM_QMemGlobalDescRam);
694     //get timer running
695 #ifdef CORTEX_A8
696     netapip_initTimer();
697     netapi_Log("netapip_systemInit: returned from netapip_initTimer\n");
698 #endif
700     /* Initialize Queue Manager Sub System */
701     result = netapip_initQm (netapi_global.cfg.def_max_descriptors); 
702     
703     if (result != 1)
704     {
705          netapi_Log("netapip_systemInit: returned from netapip_initQm with failure\n");
706         return -1;
707     }
709     /* Start the QMSS. */
710     if (netapip_startQm() != 1)
711     {
712       netapi_Log("netapip_systemInit: returned from netapip_startQm with failure\n");
713         return -1;
714     }
715     
717     //clean our old junk in 1st bunch of queues that will be allocated to us
718     netapip_cleanupAtStart();
719  netapi_Log("netapip_systemInit: returned from netapip_cleanupAtStart\n");
720     /* Initialize the global descriptor memory region. */
721     result= netapip_qmSetupMemRegion( 
722                       netapi_global.cfg.def_tot_descriptors_for_us,
723                       TUNE_NETAPI_DESC_SIZE,
724                       (unsigned int *) netapi_VM_QMemGlobalDescRam,
725                       TUNE_NETAPI_QM_GLOBAL_REGION);
726     if(result <0) {netapi_Log("netapip_systemInit: can't setup QM shared region\n"); return -1;}
728 netapi_Log("netapip_systemInit: returned from netapip_qmSetupMemRegion\n");
729     /* Initialize CPPI CPDMA */
731     result = netapip_initCppi ();
732     netapi_Log("netapip_systemInit: returned from netapip_initCppi\n");
733     if (result != 1)
734     {
735         netapi_Log ("netapi: Error initializing CPPI SubSystem error code : %d\n",result);
736         return -1;
737     }
739     /* CPPI and Queue Manager are initialized. */
740     netapi_Log ("netapi: Queue Manager and CPPI are initialized.\n");
742     /* create main pkt heap */
743     /* Initialize the Shared Heaps. */
744     Pktlib_sharedHeapInit();
745     netapi_Log("netapip_systemInit: returned from Pktlib_sharedHeapInit\n");
746     /* Populate the heap interface table. */
747     netapi_pktlib_ifTable.data_malloc             = netapip_sharedMemoryMalloc;
748     netapi_pktlib_ifTable.data_free               = netapip_sharedMemoryFree;
750     /* Initialize the heap configuration. */
751     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
752     /* Populate the heap configuration */
753     heapCfg.name                = "netapi";
754     heapCfg.memRegion           = TUNE_NETAPI_QM_GLOBAL_REGION;
755     heapCfg.sharedHeap          = 1;
756     heapCfg.useStarvationQueue  = 0;
757     heapCfg.dataBufferSize      = netapi_global.cfg.def_heap_buf_size;
758     heapCfg.numPkts             = netapi_global.cfg.def_heap_n_descriptors;
759     heapCfg.numZeroBufferPackets= netapi_global.cfg.def_heap_n_zdescriptors;
760     heapCfg.heapInterfaceTable.data_malloc  = netapi_pktlib_ifTable.data_malloc;
761     heapCfg.heapInterfaceTable.data_free    = netapi_pktlib_ifTable.data_free;
762     heapCfg.dataBufferPktThreshold   = 0;
763     heapCfg.zeroBufferPktThreshold   = 0;
765     /* Create Shared Heap with specified configuration. */
766     sharedHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
767      netapi_Log("netapip_systemInit: returned from Pktlib_createHeap1\n");
768     //todo -> cleanup on failure
769     if (!sharedHeapHandle) { netapi_Log(">'netapi' heap create failed, Error Code: %d\n",errCode); return -1;}
770     handle->netcp_heap= sharedHeapHandle;
772     /* Update for Control */
773     heapCfg.name                = "netapi_control_rx";
774     heapCfg.sharedHeap          = 1;
775     heapCfg.dataBufferSize      = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
776     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_NUM_CTL_RX_BUF;
777     heapCfg.numZeroBufferPackets= 0;
779     controlRxHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
780      netapi_Log("netapip_systemInit: returned from Pktlib_createHeap2\n");
781    //todo -> cleanup on failure
782    if (!controlRxHeapHandle) { netapi_Log("netapi -'netapi_control_rx' heap create failed, Error Code: %d\n",errCode); return -1;}
783    handle->netcp_control_rx_heap= controlRxHeapHandle;
785    
786    heapCfg.name                = "netapi_control_tx";
787    heapCfg.numPkts             = TUNE_NETAPI_CONFIG_NUM_CTL_TX_BUF;
789    controlTxHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
790     netapi_Log("netapip_systemInit: returned from Pktlib_createHeap3\n");
791    //todo -> cleanup on failure
792    if (!controlTxHeapHandle) { netapi_Log("netapi -'netapi_control_tx' heap create failed, Error Code: %d\n",errCode); return -1;}
793    handle->netcp_control_tx_heap= controlTxHeapHandle;
795     /* now NWAL */
796     result = netapip_initNwal(
797                               TUNE_NETAPI_QM_GLOBAL_REGION,
798                               &netapi_pktlib_ifTable, 
799                               &netapi_global.nwal_context,
800                               &netapi_global.cfg);
801     if (result<0) {netapi_Log("netapi  init_nwal() failed\n"); return -1; }
802  netapi_Log("netapip_systemInit: returned from netapip_initNwal\n");
803     /* start NWAL */
806     /* Common Initialization for all cores */
807     while(count < TUNE_NETAPI_MAX_NUM_TRANS)
808     {
809         netapi_global.nwal_context.transInfos[count].transId = count;
810         count++;
811     }
812     result = netapip_startNwal(sharedHeapHandle, 
813                                controlRxHeapHandle,
814                                controlTxHeapHandle,
815                                &handle->nwal_local,
816                                &netapi_global.cfg,
817                                &netapi_global.nwal_context);
818     if (result<0) {netapi_Log("netapi start_nwal() failed\n"); return -1; }
819     netapi_Log("netapip_systemInit: returned from netapip_startNwal\n");
820     return 0;
824 /****************************************************************************
825 * FUNCTION PURPOSE:  NETAPI internal function which performs clean for linux user space
826  ****************************************************************************
827  * DESCRIPTION:  NETAPI internal function which performs clean for linux user space
828  ***************************************************************************/
829 void netapip_zapQ(int queueNum)
831     char * descPtr;
832     int i;
833     if (!queueNum) 
834     {
835         return;
836     }
837     for (i=0;;i+=1 )
838     {
839         /* Pop descriptor from source queue */
840         if ((descPtr = (char *)pktio_mQmssQueuePopRaw(queueNum)) == NULL)
841         {
842             break;
843         }
844     }
845     if(i)
846     {
847         netapi_Log("netapip_zapQ: @recovery -  %d descriptors cleaned from qn %d\n",i, queueNum);
848     }
852 /****************************************************************************
853 * FUNCTION PURPOSE:  NETAPI internal function to display internal heap stats.
854  ****************************************************************************
855  * DESCRIPTION:  NETAPI internal function to display internal heap stats.
856  ***************************************************************************/
857 void netapi_dump_internal_heap_stats(void)
859     Pktlib_HeapStats    pktLibHeapStats;
860     Pktlib_getHeapStats(netapi_get_global()->nwal_context.pa2sa_heap,&pktLibHeapStats);
861     printf("PA2SA(ingress) stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
862                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
863     printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
864                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
865                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
866 #if 0
867 Pktlib_getHeapStats(netapi_get_global()->nwal_context.pa2saTX_heap,&pktLibHeapStats);
868 netapi_Log("PA2SA(egress) stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
869                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
870 netapi_Log("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
871                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
872                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
873 #endif
874     Pktlib_getHeapStats(netapi_get_global()->nwal_context.sa2pa_heap,&pktLibHeapStats);
875     printf("SA2PA stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
876                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
877     printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
878                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
879                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);