Set qmQueMgmtProxyDataReg to NULL for k1, k2h
[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>
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);
201 /* todo: Check if below size information can be made available from pa interface file */
202 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0      128
203 /* PA instance */
204 /* Memory used for the PA Instance. Needs to be assigned global uncached memory for chip */
205 uint8_t paBuf0[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0]ALIGN(CACHE_LINESZ);
207 /* Memory used for PA handles */
208 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1    128 * TUNE_NETAPI_MAX_NUM_MAC
209 uint8_t paBuf1[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1]ALIGN(CACHE_LINESZ);
211 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2   13824
214 uint8_t paBuf2[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2]ALIGN(CACHE_LINESZ);
216 /* Memory used for SA LLD global Handle */
217 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE    512
218 uint8_t salldHandle[NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE]ALIGN(CACHE_LINESZ);
220 #if 0  //need to alloc this since we need phy addr also 
221 /* Memory used for SA contet Handle */
222 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_CONTEXT_PER_CHAN             384
223 uint8_t saContext[NETAPI_NWAL_CONFIG_BUFSIZE_SA_CONTEXT_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS]ALIGN(CACHE_LINESZ);
224 #endif
226 /* Memory used by SA LLD per Channel */
227 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN          512
228 uint8_t salldChanHandle[NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2]ALIGN(CACHE_LINESZ);
231 /********************************************************************
232 * FUNCTION PURPOSE:  NETAPI internal function to gracefully cleanup when startup
233 *                                  issue occurs.
234  ********************************************************************
235  * DESCRIPTION:  NETAPI internal function to gracefully cleanup when startup
236 *                                  issue occurs.
237  ********************************************************************/
238 void netapipErrTeardown() { netapip_cleanupAtStart(); exit(-99); }
240 /********************************************************************
241  * FUNCTION PURPOSE:  Internal NETAPI function to initialize NWAL subsystem
242  ********************************************************************
243  * DESCRIPTION:  Internal NETAPI function to initialize NWAL subsytem
244  ********************************************************************/
245 int netapip_initNwal(
246     int region2use,
247     Pktlib_HeapIfTable * p_table,
248     NETAPI_NWAL_GLOBAL_CONTEXT_T * p_nwal_context, 
249     NETAPI_CFG_T*p_cfg )
251     nwalSizeInfo_t  nwalSizeInfo;
252     nwal_RetValue   nwalRetVal;
253     nwalGlobCfg_t   nwalGlobCfg;
254     uint8_t         count;
255     int             sizes[nwal_N_BUFS];
256     int             aligns[nwal_N_BUFS];
257     void*           bases[nwal_N_BUFS];   
258     Pktlib_HeapCfg      heapCfg;
259     int32_t             errCode;
261     memset(p_nwal_context,0,sizeof( NETAPI_NWAL_GLOBAL_CONTEXT_T) );
262     memset(&nwalGlobCfg,0,sizeof(nwalGlobCfg_t ) );
265     /* Initialize Buffer Pool for NetCP PA to SA packets */
266     nwalGlobCfg.pa2SaBufPool.numBufPools = 1;
267     nwalGlobCfg.pa2SaBufPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
268     nwalGlobCfg.pa2SaBufPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
270     /* Initialize the heap configuration. */
271     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
272     /* Populate the heap configuration */
273     heapCfg.name                = "nwal PA2SA";
274     heapCfg.memRegion           = region2use;
275     heapCfg.sharedHeap          = 0;
276     heapCfg.useStarvationQueue  = 0;
277     heapCfg.dataBufferSize      = p_cfg->def_heap_buf_size;
278     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_MAX_PA_TO_SA_DESC;
279     heapCfg.numZeroBufferPackets= 0;
280     heapCfg.heapInterfaceTable.data_malloc  = p_table->data_malloc;
281     heapCfg.heapInterfaceTable.data_free    = p_table->data_free;
282     heapCfg.dataBufferPktThreshold   = 0;
283     heapCfg.zeroBufferPktThreshold   = 0;
286     nwalGlobCfg.pa2SaBufPool.bufPool[0].heapHandle = p_nwal_context->pa2sa_heap=
287                       Pktlib_createHeap(&heapCfg, &errCode);
288     if(nwalGlobCfg.pa2SaBufPool.bufPool[0].heapHandle == NULL)
289     {
290         netapi_Log ("netapip_initNwal: Pktlib_createHeap:Heap Creation Failed for PA to SA Buffer Pool, Error Code: %d\n",
291                     errCode); 
292         netapipErrTeardown();
293         return -1;
294     }
296  /* Initialize Buffer Pool for NetCP SA to PA packets */
297     nwalGlobCfg.sa2PaBufPool.numBufPools = 1;
298     nwalGlobCfg.sa2PaBufPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
299     nwalGlobCfg.sa2PaBufPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
301      /* Populate the heap configuration */
302     heapCfg.name                = "nwal SA2PA";
303     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_MAX_SA_TO_PA_DESC;
305     nwalGlobCfg.sa2PaBufPool.bufPool[0].heapHandle = p_nwal_context->sa2pa_heap=
306         Pktlib_createHeap(&heapCfg, &errCode);
307     if(nwalGlobCfg.sa2PaBufPool.bufPool[0].heapHandle == NULL)
308     {
309         netapi_Log ("netapip_initNwal: Pktlib_createHeap:Heap Creation Failed for SA to PA Buffer Pool, Error Code: %d\n",
310                      errCode); 
311         netapipErrTeardown();
312         return -1;
313     }
315     nwalGlobCfg.hopLimit = 5;/* Default TTL / Hop Limit */
316     nwalGlobCfg.paPowerOn = nwal_TRUE;
317     nwalGlobCfg.saPowerOn = nwal_TRUE;
318     nwalGlobCfg.paFwActive = nwal_TRUE;
319     nwalGlobCfg.saFwActive = nwal_FALSE;
321     /* Pick Default Physical Address */
322     nwalGlobCfg.paVirtBaseAddr = (uint32_t) netapi_VM_VirtAddr->passCfgVaddr;
323     nwalGlobCfg.saVirtBaseAddr = (uint32_t) netapi_VM_VirtAddr->passCfgVaddr +
324                                  ((uint32_t)CSL_PA_SS_CFG_CP_ACE_CFG_REGS - (uint32_t)CSL_PA_SS_CFG_REGS) ;
325     nwalGlobCfg.rxDefPktQ = QMSS_PARAM_NOT_SPECIFIED;
327     /* Get the Buffer Requirement from NWAL */
328     memset(&nwalSizeInfo,0,sizeof(nwalSizeInfo));
329     nwalSizeInfo.nMaxMacAddress = TUNE_NETAPI_MAX_NUM_MAC;
330     nwalSizeInfo.nMaxIpAddress = TUNE_NETAPI_MAX_NUM_IP;
331     nwalSizeInfo.nMaxL4Ports = TUNE_NETAPI_MAX_NUM_PORTS;
332     nwalSizeInfo.nMaxIpSecChannels = TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS;//we allocate 2 per channel
333     nwalSizeInfo.nMaxDmSecChannels = TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS;//we allocate 2 per channel
334     nwalSizeInfo.nMaxL2L3Hdr = TUNE_NETAPI_MAX_NUM_L2_L3_HDRS;
335     nwalSizeInfo.nProc = TUNE_NETAPI_NUM_CORES;
336     nwalRetVal = nwal_getBufferReq(&nwalSizeInfo,
337                                    sizes,
338                                    aligns);
339     if(nwalRetVal != nwal_OK)
340     {
341         netapi_Log ("netapip_initNwal: nwal_getBufferReq Failed %d\n", nwalRetVal);
342         return nwal_FALSE;
343     }
345 /* Check for memory size requirement and update the base */
346     count = 0;
347     bases[nwal_BUF_INDEX_INST] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)nwalInstMem);
348     if(NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE < sizes[nwal_BUF_INDEX_INST])
349     {
350         /* Resize Memory */
351         while(1);
352     }
353     count++;
355     bases[nwal_BUF_INDEX_INT_HANDLES] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)nwalHandleMem);
356     if(NWAL_CHAN_HANDLE_SIZE  < sizes[nwal_BUF_INDEX_INT_HANDLES])
357     {
358         /* Resize Memory */
359         while(1);
360     }
361     count++;
362      bases[nwal_BUF_INDEX_PA_LLD_BUF0] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paBuf0);
363     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0) < sizes[nwal_BUF_INDEX_PA_LLD_BUF0])
364     {
365         /* Resize Memory */
366         while(1);
367     }
368     count++;
370     bases[nwal_BUF_INDEX_PA_LLD_BUF1] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paBuf1);
371     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1) < sizes[nwal_BUF_INDEX_PA_LLD_BUF1])
372     {
373         /* Resize Memory */
374         while(1);
375     }
376     count++;
378     bases[nwal_BUF_INDEX_PA_LLD_BUF2] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paBuf2);
379     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2) < sizes[nwal_BUF_INDEX_PA_LLD_BUF2])
380     {
381         /* Resize Memory */
382         while(1);
383     }
384     count++;
385 #ifdef NETAPI_ENABLE_SECURITY
386     bases[nwal_BUF_INDEX_SA_LLD_HANDLE] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)salldHandle);
387     if((NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE) < sizes[nwal_BUF_INDEX_SA_LLD_HANDLE])
388     {
389         /* Resize Memory */
390         while(1);
391     }
392     count++;
394     bases[nwal_BUF_INDEX_SA_CONTEXT] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)netapi_VM_SaContextVaddr);
395     count++;
397     bases[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)salldChanHandle);
398     if((NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2) <
399         sizes[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE])
400     {
401         /* Resize Memory */
402         while(1);
403     }
404     count++;
405 #else
406     bases[nwal_BUF_INDEX_SA_LLD_HANDLE] = 0;
407     bases[nwal_BUF_INDEX_SA_CONTEXT] = 0;
408     bases[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] = 0;
409     count = count+3;
410 #endif
411     if(count != nwal_N_BUFS)
412     {
413         while(1);
414     }
416     /* Initialize NWAL module */
417     nwalRetVal = nwal_create(&nwalGlobCfg,
418                              &nwalSizeInfo,
419                              sizes,
420                              bases,
421                              &p_nwal_context->nwalInstHandle);
422     if(nwalRetVal != nwal_OK)
423     {
424         netapi_Log ("netapip_initNwal: nwal_create Failed %d\n",nwalRetVal);
425         while(1);
426     }
428     netapi_Log("netapip_initNwal- Global and Local Network initialization Successful \n");
429     return 1;
432 /********************************************************************
433  * FUNCTION PURPOSE:  Internal NETAPI function to start  NWAL 
434  ********************************************************************
435  * DESCRIPTION:  Internal NETAPI function to start NWAL, per thread/core
436  ********************************************************************/
437 int netapip_startNwal(Pktlib_HeapHandle pkt_heap,
438                       Pktlib_HeapHandle cmd_rx_heap,
439                       Pktlib_HeapHandle cmd_tx_heap,
440                       NETAPI_NWAL_LOCAL_CONTEXT_T *p,
441                       NETAPI_CFG_T *p_cfg,
442                       NETAPI_NWAL_GLOBAL_CONTEXT_T * p_nwal_glob_context  )
444     nwalLocCfg_t    nwalLocCfg;
445     int count=0;
446     static int first_time = 0;
447     nwal_RetValue       nwalRetVal;
449     memset(&nwalLocCfg,0,sizeof(nwalLocCfg));
451     /* Update the Start of Packet Offset for the default flows created 
452      * by NWAL
453      */
454     nwalLocCfg.rxSopPktOffset = p_cfg->def_flow_pkt_rx_offset;
455     nwalLocCfg.rxPktTailRoomSz = p_cfg->def_heap_tailroom_size;
457  /* Call back registration for the core */
458     nwalLocCfg.pRxPktCallBack = netapip_pktioNWALRxPktCallback;
459     nwalLocCfg.pCmdCallBack = netapip_netcpCfgNWALCmdCallBack;
460     nwalLocCfg.pPaStatsCallBack = netapip_netcpCfgNWALCmdPaStatsReply;
462     nwalLocCfg.pRxDmCallBack=  netapip_pktioNWALSBPktCallback; //sideband mode callback
464     /* Initialize Buffer Pool for Control packets from NetCP to Host */
465     nwalLocCfg.rxCtlPool.numBufPools = 1;
466     nwalLocCfg.rxCtlPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
467     nwalLocCfg.rxCtlPool.bufPool[0].bufSize = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
468     nwalLocCfg.rxCtlPool.bufPool[0].heapHandle = cmd_rx_heap;
470     /* Initialize Buffer Pool for Control packets from Host to NetCP */
471     nwalLocCfg.txCtlPool.numBufPools = 1;
472     nwalLocCfg.txCtlPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
473     nwalLocCfg.txCtlPool.bufPool[0].bufSize = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
474     nwalLocCfg.txCtlPool.bufPool[0].heapHandle =  cmd_tx_heap;
476 /* Initialize Buffer Pool for Packets from NetCP to Host */
477     nwalLocCfg.rxPktPool.numBufPools = 1;
478     nwalLocCfg.rxPktPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
479     nwalLocCfg.rxPktPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
480     nwalLocCfg.rxPktPool.bufPool[0].heapHandle = pkt_heap;
482 /* Initialize Buffer Pool for Packets from Host to NetCP */
483     nwalLocCfg.txPktPool.numBufPools = 1;
484     nwalLocCfg.txPktPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
485     nwalLocCfg.txPktPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
486     nwalLocCfg.txPktPool.bufPool[0].heapHandle = pkt_heap;
488     memcpy(&p->nwalLocCfg,&nwalLocCfg,sizeof(nwalLocCfg_t));
489     while(1)
490     {
491         nwalRetVal = nwal_start(p_nwal_glob_context->nwalInstHandle,&nwalLocCfg);
492         if(nwalRetVal == nwal_ERR_INVALID_STATE)
493         {
494             continue;
495         }
496         break;
497     }
499     if(nwalRetVal != nwal_OK)
500     {
501         netapi_Log (">nwal_start:Failed ->err %d !!!\n", nwalRetVal);
502         return -1;
503     }
504     p->state = NETAPI_NW_CXT_LOC_ACTIVE;
505     return 1;
509 /***********************************************************************
510  * FUNCTION PURPOSE:  Internal NETAPI function to initialize the 64 bit timer.
511  ***********************************************************************
512  * DESCRIPTION:  Internal NETAPI function to initialize the 64 bit timer. for tci6614 ONLY
513  **********************************************************************/
514 int netapip_initTimer(void)
516 #ifdef CORTEX_A8
517         return t64_start();
518 #endif
524 /***************************************************************************
525 * FUNCTION PURPOSE:  NETAPI internal function for virtual memory allocation.
526  ***************************************************************************
527  * DESCRIPTION:  NETAPI internal function for virtual memory allocation.
528  **************************************************************************/
530 static uint8_t* netapip_sharedMemoryMalloc(uint32_t size)
532 return  (uint8_t *)hplib_vmMemAlloc(size +netapi_global.cfg.def_heap_extra_size , 128, 0); 
535 /********************************************************************
536 * FUNCTION PURPOSE:  NETAPI internal function for virtual memory free.
537  ********************************************************************
538  * DESCRIPTION:  NETAPI internal function for virtual memory free.
539  ********************************************************************/
540 static void netapip_sharedMemoryFree(uint8_t* ptr, uint32_t size)
542     /* Do Nothing. */
543     netapi_Log("netapi Unexpected.  need to provide a free () for some reason!! \n");
544     return;
547 //defensive: clean out stuff hanging around
548 //
549 //  open a bunch of free queues and zap them
550 #define NQUEUES2CLEAR 35
551 static Qmss_QueueHnd tempQH[NQUEUES2CLEAR];
552 void netapip_cleanupAtStart(void)
554     int i;
555     uint8_t         isAllocated;
557     for(i=0;i<NQUEUES2CLEAR;i++) 
558     {
559         tempQH[i] = Qmss_queueOpen(Qmss_QueueType_GENERAL_PURPOSE_QUEUE,
560                                                   QMSS_PARAM_NOT_SPECIFIED, &isAllocated);
561         netapip_zapQ(tempQH[i]);
562     }
564     for(i=0;i<NQUEUES2CLEAR;i++)
565     {
566         Qmss_queueClose(tempQH[i]);
567     }
571 /********************************************************************
572 * FUNCTION PURPOSE:  NETAPI internal function system initialization
573  ********************************************************************
574  * DESCRIPTION:  NETAPI internal function system initialization
575  ********************************************************************/
576 int netapip_systemInit(NETAPI_HANDLE_T * handle) 
578     int32_t             result;
579     Pktlib_HeapHandle   sharedHeapHandle;
580     Pktlib_HeapHandle   controlRxHeapHandle,controlTxHeapHandle;    
581     Pktlib_HeapCfg      heapCfg;
582     int32_t             errCode;
583     int count=0;
585 #ifdef NETAPI_USE_DDR
586     /* Init attributes for DDR */
587     netapi_VM_MempoolAttr[0].attr = HPLIB_ATTR_KM_CACHED0;
588     netapi_VM_MempoolAttr[0].phys_addr = 0;
589     netapi_VM_MempoolAttr[0].size = 0;
591        /* Init attributes for un-cached MSMC */
592     netapi_VM_MempoolAttr[1].attr = HPLIB_ATTR_UN_CACHED;
593     netapi_VM_MempoolAttr[1].phys_addr = CSL_MSMC_SRAM_REGS;
594     netapi_VM_MempoolAttr[1].size = TUNE_NETAPI_PERM_MEM_SZ;
595 #else
596     netapi_VM_MempoolAttr[1].attr = HPLIB_ATTR_KM_CACHED0;
597     netapi_VM_MempoolAttr[1].phys_addr = 0;
598     netapi_VM_MempoolAttr[1].size = 0;
600        /* Init attributes for un-cached MSMC */
601     netapi_VM_MempoolAttr[0].attr = HPLIB_ATTR_UN_CACHED;
602     netapi_VM_MempoolAttr[0].phys_addr = CSL_MSMC_SRAM_REGS;
603     netapi_VM_MempoolAttr[0].size = TUNE_NETAPI_PERM_MEM_SZ;
604 #endif
605     /* initialize all the memory we are going to use
606        - chunk for buffers, descriptors
607        - memory mapped peripherals we use, such as QMSS, PA, etc */
608     result = hplib_vmInit(&netapi_VM_VirtAddr[0], 2, &netapi_VM_MempoolAttr[0]);
611     if (result == hplib_OK) netapi_Log("netapip_systemInit: - memory set  up OK\n");
612     else {netapi_Log("netapip_systemInit: - memory set up failed\n"); return -1;}
615 #ifdef NETAPI_ENABLE_SECURITY
616 #define SEC_CONTEXT_SZ 384  //not tunable
617 /* allocate 2x number of tunnels since we need one for inflow and one for data mode */
618     netapi_VM_SaContextVaddr = hplib_vmMemAlloc((TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2 *
619                                         SEC_CONTEXT_SZ), 128, 0);
620     if (!netapi_VM_SaContextVaddr)
621     {
622         netapi_Log("netapip_systemInit: Failed to map SA context memory region\n");
623         return (-1);
624     }
625     netapi_Log("netapip_systemInit: SA Memory mapped/allocated at address %p.\n", netapi_VM_SaContextVaddr);
627 #else
628    netapi_VM_SaContextVaddr= (char *) NULL;
629 #endif
631 /*  TODO: the QM regions is application specific and needs to be moved 
632     since number of regions created is appliction specific, put this in netapip_systemInit */
633     /* (3) Allocate 2 QM regions from continguous chunk above */
634     netapi_VM_QMemGlobalDescRam = 
635                                   (void *)hplib_vmMemAlloc((TUNE_NETAPI_NUM_GLOBAL_DESC *
636                                                             TUNE_NETAPI_DESC_SIZE),
637                                                              128, 0);
639     netapi_VM_QMemLocalDescRam =
640                                   (void *)hplib_vmMemAlloc((TUNE_NETAPI_NUM_LOCAL_DESC *
641                                                             TUNE_NETAPI_DESC_SIZE),
642                                                             128, 0);
643     netapi_Log("netapip_systemInit: desc region=%x global desc region=%x\n", netapi_VM_QMemLocalDescRam, netapi_VM_QMemGlobalDescRam);
645     //get timer running
646 #ifdef CORTEX_A8
647     netapip_initTimer();
648     netapi_Log("netapip_systemInit: returned from netapip_initTimer\n");
649 #endif
651     /* Initialize Queue Manager Sub System */
652     result = netapip_initQm (netapi_global.cfg.def_max_descriptors); 
653     
654     if (result != 1)
655     {
656          netapi_Log("netapip_systemInit: returned from netapip_initQm with failure\n");
657         return -1;
658     }
660     /* Start the QMSS. */
661     if (netapip_startQm() != 1)
662     {
663       netapi_Log("netapip_systemInit: returned from netapip_startQm with failure\n");
664         return -1;
665     }
666     
668     //clean our old junk in 1st bunch of queues that will be allocated to us
669     netapip_cleanupAtStart();
670  netapi_Log("netapip_systemInit: returned from netapip_cleanupAtStart\n");
671     /* Initialize the global descriptor memory region. */
672     result= netapip_qmSetupMemRegion( 
673                       netapi_global.cfg.def_tot_descriptors_for_us,
674                       TUNE_NETAPI_DESC_SIZE,
675                       (unsigned int *) netapi_VM_QMemGlobalDescRam,
676                       TUNE_NETAPI_QM_GLOBAL_REGION);
677     if(result <0) {netapi_Log("netapip_systemInit: can't setup QM shared region\n"); return -1;}
679 netapi_Log("netapip_systemInit: returned from netapip_qmSetupMemRegion\n");
680 #if 0 //todo setup 2nd region
681 /* Initialize the local memory region configuration. */
682     result= netapip_qmSetupMemRegion( 
683                       NUM_HOST_DESC,
684                       SIZE_LOCAL_DESC,
685                       netapi_VM_QMemLocalDescRam,
686                       NETAPI_LOCAL_REGION);
687     if(result <0) {netapi_Log("can't setup local region\n"); return -1;}
688 #endif
689     /* Initialize CPPI CPDMA */
691     result = netapip_initCppi ();
692     netapi_Log("netapip_systemInit: returned from netapip_initCppi\n");
693     if (result != 1)
694     {
695         netapi_Log ("netapi: Error initializing CPPI SubSystem error code : %d\n",result);
696         return -1;
697     }
699     /* CPPI and Queue Manager are initialized. */
700     netapi_Log ("netapi: Queue Manager and CPPI are initialized.\n");
702     /* create main pkt heap */
703     /* Initialize the Shared Heaps. */
704     Pktlib_sharedHeapInit();
705     netapi_Log("netapip_systemInit: returned from Pktlib_sharedHeapInit\n");
706     /* Populate the heap interface table. */
707     netapi_pktlib_ifTable.data_malloc             = netapip_sharedMemoryMalloc;
708     netapi_pktlib_ifTable.data_free               = netapip_sharedMemoryFree;
710     /* Initialize the heap configuration. */
711     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
712     /* Populate the heap configuration */
713     heapCfg.name                = "netapi";
714     heapCfg.memRegion           = TUNE_NETAPI_QM_GLOBAL_REGION;
715     heapCfg.sharedHeap          = 1;
716     heapCfg.useStarvationQueue  = 0;
717     heapCfg.dataBufferSize      = netapi_global.cfg.def_heap_buf_size;
718     heapCfg.numPkts             = netapi_global.cfg.def_heap_n_descriptors;
719     heapCfg.numZeroBufferPackets= netapi_global.cfg.def_heap_n_zdescriptors;
720     heapCfg.heapInterfaceTable.data_malloc  = netapi_pktlib_ifTable.data_malloc;
721     heapCfg.heapInterfaceTable.data_free    = netapi_pktlib_ifTable.data_free;
722     heapCfg.dataBufferPktThreshold   = 0;
723     heapCfg.zeroBufferPktThreshold   = 0;
725     /* Create Shared Heap with specified configuration. */
726     sharedHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
727      netapi_Log("netapip_systemInit: returned from Pktlib_createHeap1\n");
728     //todo -> cleanup on failure
729     if (!sharedHeapHandle) { netapi_Log(">'netapi' heap create failed, Error Code: %d\n",errCode); return -1;}
730     handle->netcp_heap= sharedHeapHandle;
732     /* Update for Control */
733     heapCfg.name                = "netapi_control_rx";
734     heapCfg.sharedHeap          = 1;
735     heapCfg.dataBufferSize      = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
736     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_NUM_CTL_RX_BUF;
737     heapCfg.numZeroBufferPackets= 0;
739     controlRxHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
740      netapi_Log("netapip_systemInit: returned from Pktlib_createHeap2\n");
741    //todo -> cleanup on failure
742    if (!controlRxHeapHandle) { netapi_Log("netapi -'netapi_control_rx' heap create failed, Error Code: %d\n",errCode); return -1;}
743    handle->netcp_control_rx_heap= controlRxHeapHandle;
745    
746    heapCfg.name                = "netapi_control_tx";
747    heapCfg.numPkts             = TUNE_NETAPI_CONFIG_NUM_CTL_TX_BUF;
749    controlTxHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
750     netapi_Log("netapip_systemInit: returned from Pktlib_createHeap3\n");
751    //todo -> cleanup on failure
752    if (!controlTxHeapHandle) { netapi_Log("netapi -'netapi_control_tx' heap create failed, Error Code: %d\n",errCode); return -1;}
753    handle->netcp_control_tx_heap= controlTxHeapHandle;
755     /* now NWAL */
756     result = netapip_initNwal(
757                               TUNE_NETAPI_QM_GLOBAL_REGION,
758                               &netapi_pktlib_ifTable, 
759                               &netapi_global.nwal_context,
760                               &netapi_global.cfg);
761     if (result<0) {netapi_Log("netapi  init_nwal() failed\n"); return -1; }
762  netapi_Log("netapip_systemInit: returned from netapip_initNwal\n");
763     /* start NWAL */
766     /* Common Initialization for all cores */
767     while(count < TUNE_NETAPI_MAX_NUM_TRANS)
768     {
769         netapi_global.nwal_context.transInfos[count].transId = count;
770         count++;
771     }
772     result = netapip_startNwal(sharedHeapHandle, 
773                                controlRxHeapHandle,
774                                controlTxHeapHandle,
775                                &handle->nwal_local,
776                                &netapi_global.cfg,
777                                &netapi_global.nwal_context);
778     if (result<0) {netapi_Log("netapi start_nwal() failed\n"); return -1; }
779     netapi_Log("netapip_systemInit: returned from netapip_startNwal\n");
780     return 0;
784 /****************************************************************************
785 * FUNCTION PURPOSE:  NETAPI internal function which performs clean for linux user space
786  ****************************************************************************
787  * DESCRIPTION:  NETAPI internal function which performs clean for linux user space
788  ***************************************************************************/
789 void netapip_zapQ(int queueNum)
791     char * descPtr;
792     int i;
793     if (!queueNum) 
794     {
795         return;
796     }
797     for (i=0;;i+=1 )
798     {
799         /* Pop descriptor from source queue */
800         if ((descPtr = (char *)pktio_mQmssQueuePopRaw(queueNum)) == NULL)
801         {
802             break;
803         }
804     }
805     if(i)
806     {
807         netapi_Log("netapip_zapQ: @recovery -  %d descriptors cleaned from qn %d\n",i, queueNum);
808     }
812 /****************************************************************************
813 * FUNCTION PURPOSE:  NETAPI internal function to display internal heap stats.
814  ****************************************************************************
815  * DESCRIPTION:  NETAPI internal function to display internal heap stats.
816  ***************************************************************************/
817 void netapi_dump_internal_heap_stats(void)
819     Pktlib_HeapStats    pktLibHeapStats;
820     Pktlib_getHeapStats(netapi_get_global()->nwal_context.pa2sa_heap,&pktLibHeapStats);
821     printf("PA2SA(ingress) stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
822                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
823     printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
824                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
825                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
826 #if 0
827 Pktlib_getHeapStats(netapi_get_global()->nwal_context.pa2saTX_heap,&pktLibHeapStats);
828 netapi_Log("PA2SA(egress) stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
829                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
830 netapi_Log("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
831                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
832                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
833 #endif
834     Pktlib_getHeapStats(netapi_get_global()->nwal_context.sa2pa_heap,&pktLibHeapStats);
835     printf("SA2PA stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
836                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
837     printf("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
838                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
839                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);