90187930427ae2dff10f94eeecddceea50b0602b
[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 (">function setup_qm_region: Qmss_insertMemoryRegion returned error code %d\n", result);
157       return (-1);
158     }
160     return 1;
164 /********************************************************************
165  * FUNCTION PURPOSE:  Internal NETAPI function to start QM
166  ********************************************************************
167  * DESCRIPTION:  Internal NETAPI function to start QM
168  *               once per thread/core
169  ********************************************************************/
170 int netapip_startQm(void)
172      int32_t          result;
173      result = Qmss_start();
174      if (result != QMSS_SOK)
175      {
176          netapi_Log (">start_qm: Qmss_start failed with error code %d\n", result);
177          return (-1);
178      }
179      return 1;
183 /*** NWAL Memory Buffer Configuration ***/
184 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE     3400
185 uint8_t nwalInstMem[NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE]ALIGN(CACHE_LINESZ);
187 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_MAC                    128
188 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_IPSEC_HANDLE_PER_CHAN      256
189 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_IP                     128
190 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_PORT                   128
191 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_L2L3_HDR               128
192 #define NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_LOC_CONTEXT            384
193 #define NWAL_CHAN_HANDLE_SIZE    ((NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_MAC * TUNE_NETAPI_MAX_NUM_MAC) + \
194                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_IPSEC_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2) + \
195                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_IP * TUNE_NETAPI_MAX_NUM_IP) + \
196                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_PORT * TUNE_NETAPI_MAX_NUM_PORTS)+ \
197                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_LOC_CONTEXT * TUNE_NETAPI_NUM_CORES) + \
198                                   (NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_PER_L2L3_HDR * TUNE_NETAPI_MAX_NUM_L2_L3_HDRS))
200 uint8_t nwalHandleMem[NWAL_CHAN_HANDLE_SIZE]ALIGN(CACHE_LINESZ);
202 /* todo: Check if below size information can be made available from pa interface file */
203 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0      128
204 /* PA instance */
205 /* Memory used for the PA Instance. Needs to be assigned global uncached memory for chip */
206 uint8_t paBuf0[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0]ALIGN(CACHE_LINESZ);
208 /* Memory used for PA handles */
209 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1    128 * TUNE_NETAPI_MAX_NUM_MAC
210 uint8_t paBuf1[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1]ALIGN(CACHE_LINESZ);
212 //#define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2   768 
213 //#define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2   6144
214 #define NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2   12288
217 uint8_t paBuf2[NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2]ALIGN(CACHE_LINESZ);
219 /* Memory used for SA LLD global Handle */
220 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE    384
221 uint8_t salldHandle[NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE]ALIGN(CACHE_LINESZ);
223 #if 0  //need to alloc this since we need phy addr also 
224 /* Memory used for SA contet Handle */
225 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_CONTEXT_PER_CHAN             384
226 uint8_t saContext[NETAPI_NWAL_CONFIG_BUFSIZE_SA_CONTEXT_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS]ALIGN(CACHE_LINESZ);
227 #endif
229 /* Memory used by SA LLD per Channel */
230 #define NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN          512
231 uint8_t salldChanHandle[NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2]ALIGN(CACHE_LINESZ);
234 /********************************************************************
235 * FUNCTION PURPOSE:  NETAPI internal function to gracefully cleanup when startup
236 *                                  issue occurs.
237  ********************************************************************
238  * DESCRIPTION:  NETAPI internal function to gracefully cleanup when startup
239 *                                  issue occurs.
240  ********************************************************************/
241 void netapipErrTeardown() { netapip_cleanupAtStart(); exit(-99); }
243 /********************************************************************
244  * FUNCTION PURPOSE:  Internal NETAPI function to initialize NWAL subsystem
245  ********************************************************************
246  * DESCRIPTION:  Internal NETAPI function to initialize NWAL subsytem
247  ********************************************************************/
248 int netapip_initNwal(
249     int region2use,
250     Pktlib_HeapIfTable * p_table,
251     NETAPI_NWAL_GLOBAL_CONTEXT_T * p_nwal_context, 
252     NETAPI_CFG_T*p_cfg )
254     nwalSizeInfo_t  nwalSizeInfo;
255     nwal_RetValue   nwalRetVal;
256     nwalGlobCfg_t   nwalGlobCfg;
257     uint8_t         count;
258     int             sizes[nwal_N_BUFS];
259     int             aligns[nwal_N_BUFS];
260     void*           bases[nwal_N_BUFS];   
261     Pktlib_HeapCfg      heapCfg;
262     int32_t             errCode;
264     memset(p_nwal_context,0,sizeof( NETAPI_NWAL_GLOBAL_CONTEXT_T) );
265     memset(&nwalGlobCfg,0,sizeof(nwalGlobCfg_t ) );
268     /* Initialize Buffer Pool for NetCP PA to SA packets */
269     nwalGlobCfg.pa2SaBufPool.numBufPools = 1;
270     nwalGlobCfg.pa2SaBufPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
271     nwalGlobCfg.pa2SaBufPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
273     /* Initialize the heap configuration. */
274     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
275     /* Populate the heap configuration */
276     heapCfg.name                = "nwal PA2SA";
277     heapCfg.memRegion           = region2use;
278     heapCfg.sharedHeap          = 0;
279     heapCfg.useStarvationQueue  = 0;
280     heapCfg.dataBufferSize      = p_cfg->def_heap_buf_size;
281     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_MAX_PA_TO_SA_DESC;
282     heapCfg.numZeroBufferPackets= 0;
283     heapCfg.heapInterfaceTable.data_malloc  = p_table->data_malloc;
284     heapCfg.heapInterfaceTable.data_free    = p_table->data_free;
285     heapCfg.dataBufferPktThreshold   = 0;
286     heapCfg.zeroBufferPktThreshold   = 0;
289     nwalGlobCfg.pa2SaBufPool.bufPool[0].heapHandle = p_nwal_context->pa2sa_heap=
290                       Pktlib_createHeap(&heapCfg, &errCode);
291     if(nwalGlobCfg.pa2SaBufPool.bufPool[0].heapHandle == NULL)
292     {
293         netapi_Log (">Pktlib_createHeap:Heap Creation Failed for PA to SA Buffer Pool , Error Code: %d\n",errCode); 
294         netapipErrTeardown();
295         return -1;
296     }
298  /* Initialize Buffer Pool for NetCP SA to PA packets */
299     nwalGlobCfg.sa2PaBufPool.numBufPools = 1;
300     nwalGlobCfg.sa2PaBufPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
301     nwalGlobCfg.sa2PaBufPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
303      /* Populate the heap configuration */
304     heapCfg.name                = "nwal SA2PA";
305     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_MAX_SA_TO_PA_DESC;
307     nwalGlobCfg.sa2PaBufPool.bufPool[0].heapHandle = p_nwal_context->sa2pa_heap=
308         Pktlib_createHeap(&heapCfg, &errCode);
309     if(nwalGlobCfg.sa2PaBufPool.bufPool[0].heapHandle == NULL)
310     {
311         netapi_Log (">Pktlib_createHeap:Heap Creation Failed for SA to PA Buffer Pool  , Error Code: %d\n",errCode); 
312         netapipErrTeardown();
313         return -1;
314     }
316     nwalGlobCfg.hopLimit = 5;/* Default TTL / Hop Limit */
317     nwalGlobCfg.paPowerOn = nwal_TRUE;
318     nwalGlobCfg.saPowerOn = nwal_TRUE;
319     nwalGlobCfg.paFwActive = nwal_TRUE;
320     nwalGlobCfg.saFwActive = nwal_FALSE;
322     /* Pick Default Physical Address */
323     nwalGlobCfg.paVirtBaseAddr = (uint32_t) netapi_VM_VirtAddr->passCfgVaddr;
324     nwalGlobCfg.saVirtBaseAddr = (uint32_t) netapi_VM_VirtAddr->passCfgVaddr +
325                                  ((uint32_t)CSL_PA_SS_CFG_CP_ACE_CFG_REGS - (uint32_t)CSL_PA_SS_CFG_REGS) ;
326     nwalGlobCfg.rxDefPktQ = QMSS_PARAM_NOT_SPECIFIED;
328     /* Get the Buffer Requirement from NWAL */
329     memset(&nwalSizeInfo,0,sizeof(nwalSizeInfo));
330     nwalSizeInfo.nMaxMacAddress = TUNE_NETAPI_MAX_NUM_MAC;
331     nwalSizeInfo.nMaxIpAddress = TUNE_NETAPI_MAX_NUM_IP;
332     nwalSizeInfo.nMaxL4Ports = TUNE_NETAPI_MAX_NUM_PORTS;
333     nwalSizeInfo.nMaxIpSecChannels = TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS;//we allocate 2 per channel
334     nwalSizeInfo.nMaxDmSecChannels = TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS;//we allocate 2 per channel
335     nwalSizeInfo.nMaxL2L3Hdr = TUNE_NETAPI_MAX_NUM_L2_L3_HDRS;
336     nwalSizeInfo.nProc = TUNE_NETAPI_NUM_CORES;
337     nwalRetVal = nwal_getBufferReq(&nwalSizeInfo,
338                                    sizes,
339                                    aligns);
340     if(nwalRetVal != nwal_OK)
341     {
342         netapi_Log ("netapi: init_nwal - nwal_getBufferReq Failed %d\n", nwalRetVal);
343         return nwal_FALSE;
344     }
346 /* Check for memory size requirement and update the base */
347     count = 0;
348     bases[nwal_BUF_INDEX_INST] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)nwalInstMem);
349     if(NETAPI_NWAL_CONFIG_BUFSIZE_NWAL_HANDLE < sizes[nwal_BUF_INDEX_INST])
350     {
351         /* Resize Memory */
352         while(1);
353     }
354     count++;
356     bases[nwal_BUF_INDEX_INT_HANDLES] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)nwalHandleMem);
357     if(NWAL_CHAN_HANDLE_SIZE  < sizes[nwal_BUF_INDEX_INT_HANDLES])
358     {
359         netapi_Log("one\n");
360         /* Resize Memory */
361         while(1);
362     }
363     count++;
364      bases[nwal_BUF_INDEX_PA_LLD_BUF0] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paBuf0);
365     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF0) < sizes[nwal_BUF_INDEX_PA_LLD_BUF0])
366     {
367         /* Resize Memory */
368         netapi_Log("two\n");
369         while(1);
370     }
371     count++;
373     bases[nwal_BUF_INDEX_PA_LLD_BUF1] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paBuf1);
374     netapi_Log("NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1 %d, size[nwal_BUF_INDEX_PA_LLD_BUF1] %d\n",
375         NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1, sizes[nwal_BUF_INDEX_PA_LLD_BUF1]);
376     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1) < sizes[nwal_BUF_INDEX_PA_LLD_BUF1])
377     {
378         netapi_Log("NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1 %d, size[nwal_BUF_INDEX_PA_LLD_BUF1] %d\n",
379             NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF1, sizes[nwal_BUF_INDEX_PA_LLD_BUF1]);
380         /* Resize Memory */
381         while(1);
382     }
383     count++;
385     bases[nwal_BUF_INDEX_PA_LLD_BUF2] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)paBuf2);
386     if((NETAPI_NWAL_CONFIG_BUFSIZE_PA_BUF2) < sizes[nwal_BUF_INDEX_PA_LLD_BUF2])
387     {
388         netapi_Log("four\n");
389         /* Resize Memory */
390         while(1);
391     }
392     count++;
393 #ifdef NETAPI_ENABLE_SECURITY
394     bases[nwal_BUF_INDEX_SA_LLD_HANDLE] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)salldHandle);
395     if((NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE) < sizes[nwal_BUF_INDEX_SA_LLD_HANDLE])
396     {
397         netapi_Log("five\n");
398         /* Resize Memory */
399         while(1);
400     }
401     count++;
403     bases[nwal_BUF_INDEX_SA_CONTEXT] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)netapi_VM_SaContextVaddr);
404     count++;
406     bases[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] = (uint32_t *)Osal_nwalLocToGlobAddr((uint32_t)salldChanHandle);
407     if((NETAPI_NWAL_CONFIG_BUFSIZE_SA_LLD_HANDLE_PER_CHAN * TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2) <
408         sizes[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE])
409     {
410         netapi_Log("netapip_initNwal:  sizes[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] %d\n",  sizes[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE]);
411         /* Resize Memory */
412         while(1);
413     }
414     count++;
415 #else
416     bases[nwal_BUF_INDEX_SA_LLD_HANDLE] = 0;
417     bases[nwal_BUF_INDEX_SA_CONTEXT] = 0;
418     bases[nwal_BUF_INDEX_SA_LLD_CHAN_HANDLE] = 0;
419     count = count+3;
420 #endif
421     if(count != nwal_N_BUFS)
422     {
423         while(1);
424     }
426     /* Initialize NWAL module */
427     nwalRetVal = nwal_create(&nwalGlobCfg,
428                              &nwalSizeInfo,
429                              sizes,
430                              bases,
431                              &p_nwal_context->nwalInstHandle);
432     if(nwalRetVal != nwal_OK)
433     {
434         netapi_Log ("netapi: init_nwal- nwal_create Failed %d\n",nwalRetVal);
435         while(1);
436     }
438     netapi_Log("netapi: init_nwal - Global and Local Network initialization Successful \n");
439     return 1;
442 /********************************************************************
443  * FUNCTION PURPOSE:  Internal NETAPI function to start  NWAL 
444  ********************************************************************
445  * DESCRIPTION:  Internal NETAPI function to start NWAL, per thread/core
446  ********************************************************************/
447 int netapip_startNwal(Pktlib_HeapHandle pkt_heap,
448                       Pktlib_HeapHandle cmd_rx_heap,
449                       Pktlib_HeapHandle cmd_tx_heap,
450                       NETAPI_NWAL_LOCAL_CONTEXT_T *p,
451                       NETAPI_CFG_T *p_cfg,
452                       NETAPI_NWAL_GLOBAL_CONTEXT_T * p_nwal_glob_context  )
454     nwalLocCfg_t    nwalLocCfg;
455     int count=0;
456     static int first_time = 0;
457     nwal_RetValue       nwalRetVal;
459     memset(&nwalLocCfg,0,sizeof(nwalLocCfg));
461     /* Update the Start of Packet Offset for the default flows created 
462      * by NWAL
463      */
464     nwalLocCfg.rxSopPktOffset = p_cfg->def_flow_pkt_rx_offset;
465     nwalLocCfg.rxPktTailRoomSz = p_cfg->def_heap_tailroom_size;
467  /* Call back registration for the core */
468     nwalLocCfg.pRxPktCallBack = netapip_pktioNWALRxPktCallback;
469     nwalLocCfg.pCmdCallBack = netapip_netcpCfgNWALCmdCallBack;
470     nwalLocCfg.pPaStatsCallBack = netapip_netcpCfgNWALCmdPaStatsReply;
472     nwalLocCfg.pRxDmCallBack=  netapip_pktioNWALSBPktCallback; //sideband mode callback
474     /* Initialize Buffer Pool for Control packets from NetCP to Host */
475     nwalLocCfg.rxCtlPool.numBufPools = 1;
476     nwalLocCfg.rxCtlPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
477     nwalLocCfg.rxCtlPool.bufPool[0].bufSize = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
478     nwalLocCfg.rxCtlPool.bufPool[0].heapHandle = cmd_rx_heap;
480     /* Initialize Buffer Pool for Control packets from Host to NetCP */
481     nwalLocCfg.txCtlPool.numBufPools = 1;
482     nwalLocCfg.txCtlPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
483     nwalLocCfg.txCtlPool.bufPool[0].bufSize = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
484     nwalLocCfg.txCtlPool.bufPool[0].heapHandle =  cmd_tx_heap;
486 /* Initialize Buffer Pool for Packets from NetCP to Host */
487     nwalLocCfg.rxPktPool.numBufPools = 1;
488     nwalLocCfg.rxPktPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
489     nwalLocCfg.rxPktPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
490     nwalLocCfg.rxPktPool.bufPool[0].heapHandle = pkt_heap;
492 /* Initialize Buffer Pool for Packets from Host to NetCP */
493     nwalLocCfg.txPktPool.numBufPools = 1;
494     nwalLocCfg.txPktPool.bufPool[0].descSize = TUNE_NETAPI_DESC_SIZE;
495     nwalLocCfg.txPktPool.bufPool[0].bufSize =  p_cfg->def_heap_buf_size;
496     nwalLocCfg.txPktPool.bufPool[0].heapHandle = pkt_heap;
498     memcpy(&p->nwalLocCfg,&nwalLocCfg,sizeof(nwalLocCfg_t));
499     while(1)
500     {
501         nwalRetVal = nwal_start(p_nwal_glob_context->nwalInstHandle,&nwalLocCfg);
502         if(nwalRetVal == nwal_ERR_INVALID_STATE)
503         {
504             continue;
505         }
506         break;
507     }
509     if(nwalRetVal != nwal_OK)
510     {
511         netapi_Log (">nwal_start:Failed ->err %d !!!\n", nwalRetVal);
512         return -1;
513     }
514     p->state = NETAPI_NW_CXT_LOC_ACTIVE;
515     return 1;
519 /***********************************************************************
520  * FUNCTION PURPOSE:  Internal NETAPI function to initialize the 64 bit timer.
521  ***********************************************************************
522  * DESCRIPTION:  Internal NETAPI function to initialize the 64 bit timer. for tci6614 ONLY
523  **********************************************************************/
524 int netapip_initTimer(void)
526 #ifdef CORTEX_A8
527         return t64_start();
528 #endif
534 /***************************************************************************
535 * FUNCTION PURPOSE:  NETAPI internal function for virtual memory allocation.
536  ***************************************************************************
537  * DESCRIPTION:  NETAPI internal function for virtual memory allocation.
538  **************************************************************************/
540 static uint8_t* netapip_sharedMemoryMalloc(uint32_t size)
542 return  (uint8_t *)hplib_vmMemAlloc(size +netapi_global.cfg.def_heap_extra_size , 128, 0); 
545 /********************************************************************
546 * FUNCTION PURPOSE:  NETAPI internal function for virtual memory free.
547  ********************************************************************
548  * DESCRIPTION:  NETAPI internal function for virtual memory free.
549  ********************************************************************/
550 static void netapip_sharedMemoryFree(uint8_t* ptr, uint32_t size)
552     /* Do Nothing. */
553     netapi_Log("netapi Unexpected.  need to provide a free () for some reason!! \n");
554     return;
557 //defensive: clean out stuff hanging around
558 //
559 //  open a bunch of free queues and zap them
560 #define NQUEUES2CLEAR 35
561 static Qmss_QueueHnd tempQH[NQUEUES2CLEAR];
562 void netapip_cleanupAtStart(void)
564     int i;
565     uint8_t         isAllocated;
567     for(i=0;i<NQUEUES2CLEAR;i++) 
568     {
569         tempQH[i] = Qmss_queueOpen(Qmss_QueueType_GENERAL_PURPOSE_QUEUE,
570                                                   QMSS_PARAM_NOT_SPECIFIED, &isAllocated);
571         netapip_zapQ(tempQH[i]);
572     }
574     for(i=0;i<NQUEUES2CLEAR;i++)
575     {
576         Qmss_queueClose(tempQH[i]);
577     }
581 /********************************************************************
582 * FUNCTION PURPOSE:  NETAPI internal function system initialization
583  ********************************************************************
584  * DESCRIPTION:  NETAPI internal function system initialization
585  ********************************************************************/
586 int netapip_systemInit(NETAPI_HANDLE_T * handle) 
588     int32_t             result;
589     Pktlib_HeapHandle   sharedHeapHandle;
590     Pktlib_HeapHandle   controlRxHeapHandle,controlTxHeapHandle;    
591     Pktlib_HeapCfg      heapCfg;
592     int32_t             errCode;
593     int count=0;
595 #ifdef NETAPI_USE_DDR
596     /* Init attributes for DDR */
597     netapi_VM_MempoolAttr[0].attr = HPLIB_ATTR_KM_CACHED0;
598     netapi_VM_MempoolAttr[0].phys_addr = 0;
599     netapi_VM_MempoolAttr[0].size = 0;
601        /* Init attributes for un-cached MSMC */
602     netapi_VM_MempoolAttr[1].attr = HPLIB_ATTR_UN_CACHED;
603     netapi_VM_MempoolAttr[1].phys_addr = CSL_MSMC_SRAM_REGS;
604     netapi_VM_MempoolAttr[1].size = TUNE_NETAPI_PERM_MEM_SZ;
605 #else
606     netapi_VM_MempoolAttr[1].attr = HPLIB_ATTR_KM_CACHED0;
607     netapi_VM_MempoolAttr[1].phys_addr = 0;
608     netapi_VM_MempoolAttr[1].size = 0;
610        /* Init attributes for un-cached MSMC */
611     netapi_VM_MempoolAttr[0].attr = HPLIB_ATTR_UN_CACHED;
612     netapi_VM_MempoolAttr[0].phys_addr = CSL_MSMC_SRAM_REGS;
613     netapi_VM_MempoolAttr[0].size = TUNE_NETAPI_PERM_MEM_SZ;
614 #endif
615     /* initialize all the memory we are going to use
616        - chunk for buffers, descriptors
617        - memory mapped peripherals we use, such as QMSS, PA, etc */
618     result = hplib_vmInit(&netapi_VM_VirtAddr[0], 2, &netapi_VM_MempoolAttr[0]);
620     /* TODO: at this point, we need to create the QM regions which need to moved out of the above 
621     netapi_VM_memory_setup() call, also need to move out the SA context stuff */
623     if (result == hplib_OK) netapi_Log("netapi: system init - memory set  up OK\n");
624     else {netapi_Log(">netap: system init - memory set up failed\n"); return -1;}
627 #ifdef NETAPI_ENABLE_SECURITY
628 #define SEC_CONTEXT_SZ 384  //not tunable
629 /* allocate 2x number of tunnels since we need one for inflow and one for data mode */
630     netapi_VM_SaContextVaddr = hplib_vmMemAlloc((TUNE_NETAPI_MAX_NUM_IPSEC_CHANNELS*2 *
631                                         SEC_CONTEXT_SZ), 128, 0);
632     if (!netapi_VM_SaContextVaddr)
633     {
634         netapi_Log("netapi ERROR: Failed to map SA context memory region\n");
635         return (-1);
636     }
637     netapi_Log(">hplib VM_SaContext: Memory mapped/allocated at address %p.\n", netapi_VM_SaContextVaddr);
639 #else
640    netapi_VM_SaContextVaddr= (char *) NULL;
641 #endif
643 /*  TODO: the QM regions is application specific and needs to be moved 
644     since number of regions created is appliction specific, put this in netapip_systemInit */
645     /* (3) Allocate 2 QM regions from continguous chunk above */
646     netapi_VM_QMemGlobalDescRam = 
647                                                             (void *)hplib_vmMemAlloc((TUNE_NETAPI_NUM_GLOBAL_DESC *
648                                                                                                         TUNE_NETAPI_DESC_SIZE),
649                                                                                                         128, 0);
651     netapi_VM_QMemLocalDescRam =
652                                                             (void *)hplib_vmMemAlloc((TUNE_NETAPI_NUM_LOCAL_DESC *
653                                                                                                         TUNE_NETAPI_DESC_SIZE),
654                                                                                                         128, 0);
655     netapi_Log("netapi local desc region=%x global desc region=%x\n", netapi_VM_QMemLocalDescRam, netapi_VM_QMemGlobalDescRam);
657     //get timer running
658 #ifdef CORTEX_A8
659     netapip_initTimer();
660 #endif
661      netapi_Log("netapip_systemInit: returned from netapip_initTimer\n");
663     /* Initialize Queue Manager Sub System */
664     result = netapip_initQm (netapi_global.cfg.def_max_descriptors); 
665     
666     if (result != 1)
667     {
668          netapi_Log("netapip_systemInit: returned from netapip_initQm with failure\n");
669         return -1;
670     }
672     /* Start the QMSS. */
673     if (netapip_startQm() != 1)
674     {
675       netapi_Log("netapip_systemInit: returned from netapip_startQm with failure\n");
676         return -1;
677     }
678     
680     //clean our old junk in 1st bunch of queues that will be allocated to us
681     netapip_cleanupAtStart();
682  netapi_Log("netapip_systemInit: returned from netapip_cleanupAtStart\n");
683     /* Initialize the global descriptor memory region. */
684     result= netapip_qmSetupMemRegion( 
685                       netapi_global.cfg.def_tot_descriptors_for_us,
686                       TUNE_NETAPI_DESC_SIZE,
687                       (unsigned int *) netapi_VM_QMemGlobalDescRam,
688                       TUNE_NETAPI_QM_GLOBAL_REGION);
689     if(result <0) {netapi_Log("netapi; can't setup QM shared region\n"); return -1;}
691 netapi_Log("netapip_systemInit: returned from netapip_qmSetupMemRegion\n");
692 #if 0 //todo setup 2nd region
693 /* Initialize the local memory region configuration. */
694     result= netapip_qmSetupMemRegion( 
695                       NUM_HOST_DESC,
696                       SIZE_LOCAL_DESC,
697                       netapi_VM_QMemLocalDescRam,
698                       NETAPI_LOCAL_REGION);
699     if(result <0) {netapi_Log("can't setup local region\n"); return -1;}
700 #endif
701     /* Initialize CPPI CPDMA */
703     result = netapip_initCppi ();
704     netapi_Log("netapip_systemInit: returned from netapip_initCppi\n");
705     if (result != 1)
706     {
707         netapi_Log ("netapi: Error initializing CPPI SubSystem error code : %d\n",result);
708         return -1;
709     }
711     /* CPPI and Queue Manager are initialized. */
712     netapi_Log ("netapi: Queue Manager and CPPI are initialized.\n");
714     /* create main pkt heap */
715     /* Initialize the Shared Heaps. */
716     Pktlib_sharedHeapInit();
717     netapi_Log("netapip_systemInit: returned from Pktlib_sharedHeapInit\n");
718     /* Populate the heap interface table. */
719     netapi_pktlib_ifTable.data_malloc             = netapip_sharedMemoryMalloc;
720     netapi_pktlib_ifTable.data_free               = netapip_sharedMemoryFree;
722     /* Initialize the heap configuration. */
723     memset ((void *)&heapCfg, 0, sizeof(Pktlib_HeapCfg));
724     /* Populate the heap configuration */
725     heapCfg.name                = "netapi";
726     heapCfg.memRegion           = TUNE_NETAPI_QM_GLOBAL_REGION;
727     heapCfg.sharedHeap          = 1;
728     heapCfg.useStarvationQueue  = 0;
729     heapCfg.dataBufferSize      = netapi_global.cfg.def_heap_buf_size;
730     heapCfg.numPkts             = netapi_global.cfg.def_heap_n_descriptors;
731     heapCfg.numZeroBufferPackets= netapi_global.cfg.def_heap_n_zdescriptors;
732     heapCfg.heapInterfaceTable.data_malloc  = netapi_pktlib_ifTable.data_malloc;
733     heapCfg.heapInterfaceTable.data_free    = netapi_pktlib_ifTable.data_free;
734     heapCfg.dataBufferPktThreshold   = 0;
735     heapCfg.zeroBufferPktThreshold   = 0;
737     /* Create Shared Heap with specified configuration. */
738     sharedHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
739      netapi_Log("netapip_systemInit: returned from Pktlib_createHeap1\n");
740     //todo -> cleanup on failure
741     if (!sharedHeapHandle) { netapi_Log(">'netapi' heap create failed, Error Code: %d\n",errCode); return -1;}
742     handle->netcp_heap= sharedHeapHandle;
744     /* Update for Control */
745     heapCfg.name                = "netapi_control_rx";
746     heapCfg.sharedHeap          = 1;
747     heapCfg.dataBufferSize      = TUNE_NETAPI_CONFIG_MAX_CTL_RXTX_BUF_SIZE;
748     heapCfg.numPkts             = TUNE_NETAPI_CONFIG_NUM_CTL_RX_BUF;
749     heapCfg.numZeroBufferPackets= 0;
751     controlRxHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
752      netapi_Log("netapip_systemInit: returned from Pktlib_createHeap2\n");
753    //todo -> cleanup on failure
754    if (!controlRxHeapHandle) { netapi_Log("netapi -'netapi_control_rx' heap create failed, Error Code: %d\n",errCode); return -1;}
755    handle->netcp_control_rx_heap= controlRxHeapHandle;
757    
758    heapCfg.name                = "netapi_control_tx";
759    heapCfg.numPkts             = TUNE_NETAPI_CONFIG_NUM_CTL_TX_BUF;
761    controlTxHeapHandle = Pktlib_createHeap(&heapCfg, &errCode);
762     netapi_Log("netapip_systemInit: returned from Pktlib_createHeap3\n");
763    //todo -> cleanup on failure
764    if (!controlTxHeapHandle) { netapi_Log("netapi -'netapi_control_tx' heap create failed, Error Code: %d\n",errCode); return -1;}
765    handle->netcp_control_tx_heap= controlTxHeapHandle;
767     /* now NWAL */
768     result = netapip_initNwal(
769                               TUNE_NETAPI_QM_GLOBAL_REGION,
770                               &netapi_pktlib_ifTable, 
771                               &netapi_global.nwal_context,
772                               &netapi_global.cfg);
773     if (result<0) {netapi_Log("netapi  init_nwal() failed\n"); return -1; }
774  netapi_Log("netapip_systemInit: returned from netapip_initNwal\n");
775     /* start NWAL */
778     /* Common Initialization for all cores */
779     while(count < TUNE_NETAPI_MAX_NUM_TRANS)
780     {
781         netapi_global.nwal_context.transInfos[count].transId = count;
782         count++;
783     }
784     result = netapip_startNwal(sharedHeapHandle, 
785                                controlRxHeapHandle,
786                                controlTxHeapHandle,
787                                &handle->nwal_local,
788                                &netapi_global.cfg,
789                                &netapi_global.nwal_context);
790     if (result<0) {netapi_Log("netapi start_nwal() failed\n"); return -1; }
791     netapi_Log("netapip_systemInit: returned from netapip_startNwal\n");
792     return 0;
796 /****************************************************************************
797 * FUNCTION PURPOSE:  NETAPI internal function which performs clean for linux user space
798  ****************************************************************************
799  * DESCRIPTION:  NETAPI internal function which performs clean for linux user space
800  ***************************************************************************/
801 void netapip_zapQ(int queueNum)
803     char * descPtr;
804     int i;
805     if (!queueNum) 
806     {
807         return;
808     }
809     for (i=0;;i+=1 )
810     {
811         /* Pop descriptor from source queue */
812         if ((descPtr = (char *)pktio_mQmssQueuePopRaw(queueNum)) == NULL)
813         {
814             break;
815         }
816     }
817     if(i)
818     {
819         netapi_Log("netapip_zapQ: @recovery -  %d descriptors cleaned from qn %d\n",i, queueNum);
820     }
824 /****************************************************************************
825 * FUNCTION PURPOSE:  NETAPI internal function to display internal heap stats.
826  ****************************************************************************
827  * DESCRIPTION:  NETAPI internal function to display internal heap stats.
828  ***************************************************************************/
829 void netapi_dump_internal_heap_stats(void)
831     Pktlib_HeapStats    pktLibHeapStats;
832     Pktlib_getHeapStats(netapi_get_global()->nwal_context.pa2sa_heap,&pktLibHeapStats);
833     netapi_Log("PA2SA(ingress) stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
834                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
835     netapi_Log("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
836                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
837                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
838 #if 0
839 Pktlib_getHeapStats(netapi_get_global()->nwal_context.pa2saTX_heap,&pktLibHeapStats);
840 netapi_Log("PA2SA(egress) stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
841                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
842 netapi_Log("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
843                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
844                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);
845 #endif
846     Pktlib_getHeapStats(netapi_get_global()->nwal_context.sa2pa_heap,&pktLibHeapStats);
847     netapi_Log("SA2PA stats>  #free=%d #zb=%d #garbage=%d\n", pktLibHeapStats.numFreeDataPackets,
848                                 pktLibHeapStats.numZeroBufferPackets, pktLibHeapStats.numPacketsinGarbage);
849     netapi_Log("               >  #dataBufThreshStatus=%d #dataBufStarvCounter=%d #zBufThreshStatus=%d #zBufStarvCounter=%d \n",
850                         pktLibHeapStats.dataBufferThresholdStatus,pktLibHeapStats.dataBufferStarvationCounter,
851                         pktLibHeapStats.zeroDataBufferThresholdStatus, pktLibHeapStats.zeroDataBufferStarvationCounter);