5ea355975e3d0e56a8bbbee4df0dd0ba21245bc4
[keystone-rtos/netapi.git] / ti / runtime / netapi / src / netcp_cfg.c
1 /**********************************************************
2  * file: netcp_cfg.c
3  * purpose: netcp configurations routines
4  **************************************************************
5  * FILE: netcp_cfg.c
6  * 
7  * DESCRIPTION:  netcp configuration main source file for user space transport
8  *               library
9  * 
10  * REVISION HISTORY:
11  *
12  *  Copyright (c) Texas Instruments Incorporated 2013
13  * 
14  *  Redistribution and use in source and binary forms, with or without 
15  *  modification, are permitted provided that the following conditions 
16  *  are met:
17  *
18  *    Redistributions of source code must retain the above copyright 
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  *    Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the 
23  *    documentation and/or other materials provided with the   
24  *    distribution.
25  *
26  *    Neither the name of Texas Instruments Incorporated nor the names of
27  *    its contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
31  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
32  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
34  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
35  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
36  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
39  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
40  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  ******************************************************/
44 #include "netapi.h"
46 extern NETAPI_SHM_T* pnetapiShm;
48 /******************************************************************
49  ********************Netapi internal*************************************
50 *******************************************************************/
53 /********************************************************************
54  * FUNCTION PURPOSE:  Netapi internal function to get a free transaction id
55  ********************************************************************
56  * DESCRIPTION:  Netapi internal function to get a free transaction id.
57  ********************************************************************/
58 NetapiNwalTransInfo_t * netapip_getFreeTransInfo(NETAPI_HANDLE_T *p_handle,
59                                                  NETAPI_PROC_GLOBAL_T *p_proc_global,
60                                                  nwal_TransID_t *pTransId)
61 {
62     uint16_t    count=0;
64     count=0;
65     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
66     while(count < TUNE_NETAPI_MAX_NUM_TRANS)
67     {
68         if((p_proc_global->nwal_context.transInfos[count].inUse) != nwal_TRUE)
69         {
70             p_proc_global->nwal_context.transInfos[count].inUse = nwal_TRUE;
71             *pTransId = count;
72             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
73             return(&p_proc_global->nwal_context.transInfos[count]);
74         }
75         count++;
76     }
77     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
78     /* trouble.  need to wait for one to free up*/
79     /* to do: handle this by forcing a poll of cntrl queue*/
80     return NULL;
82 }
84 /********************************************************************
85  * FUNCTION PURPOSE:  Netapi internal function to free a transaction id
86  ********************************************************************
87  * DESCRIPTION:  Netapi internal function to free a transaction id
88  ********************************************************************/
89 void netapip_freeTransInfo(NetapiNwalTransInfo_t *pTransInfo)
90 {
91     pTransInfo->inUse = nwal_FALSE;
92     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_IDLE;
93     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_NONE;
94 }
95 /********************************************************************
96  * FUNCTION PURPOSE:  Netapi internal function to build a route
97  ********************************************************************
98  * DESCRIPTION:  Netapi internal function to build a route
99  ********************************************************************/
100 void netapip_netcpCfgBuildRoute(NETCP_CFG_ROUTE_T * p_route,
101                                 int16_t *p_flow,
102                                 Qmss_QueueHnd *p_q,
103                                 nwalRouteType_t *p_type)
105     if (!p_route) return;
106     if (p_route->p_flow)  *p_flow= p_route->p_flow->flowid;
107     else *p_flow = CPPI_PARAM_NOT_SPECIFIED;
109     if (p_route->p_dest_q) *p_q = netapi_pktioGetQ(p_route->p_dest_q);
110     else *p_q=QMSS_PARAM_NOT_SPECIFIED;
112     *p_type = p_route->routeType;
116 /******************************************************/
117 /*----------------database management stuff           */
118 /******************************************************/
120 /*=====================POLICIES=============================*/
122 /********************************************************************
123  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for an SA
124  ********************************************************************
125  * DESCRIPTION:  Netapi internal function to find a free slot for an SA
126  ********************************************************************/
127 int netapip_netcpCfgFindPolicySlot(NETAPI_HANDLE_T *p_handle,
128                                    NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
129                                    int tunnel)
131     int i;
132     if ((tunnel <0 ) || (tunnel >=TUNE_NETAPI_MAX_SA))
133         return -1;
135     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
136     //find a free entry
137     for(i=0;i<TUNE_NETAPI_MAX_POLICY;i++)
138     {
139         if (!p->policy[i].in_use)
140         {
141             p->policy[i].in_use = 2; //pending
142             p->policy[i].tunnel= tunnel; //save tunnel this is linked to
143             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
144             return i;
145         }
146     }
147     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
148     return -1;
151 /********************************************************************
152  * FUNCTION PURPOSE:  Netapi internal function to delete a policy from policy list
153  ********************************************************************
154  * DESCRIPTION:  Netapi internal function to delete a policy from policy list
155  ********************************************************************/
156 void netapip_netcpCfgDeletePolicy(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
157                                   int policy_slot)
159     if ((policy_slot <0 ) || (policy_slot >= TUNE_NETAPI_MAX_POLICY))
160     {
161         return ;
162     }
163     p->policy[policy_slot].in_use=0;
164     return;
167 /********************************************************************
168  * FUNCTION PURPOSE:  Netapi internal function to insert a policy to the policy list
169  ********************************************************************
170  * DESCRIPTION:  Netapi internal function to insert a policy to the policy list
171  ********************************************************************/
172 void netapip_netcpCfgInsertPolicy(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
173                                   int policy_slot,
174                                   void * handle,
175                                   void * user_data)
177     p->policy[policy_slot].in_use=1;
178     p->policy[policy_slot].nwal_handle = handle;
179     p->policy[policy_slot].user_data = user_data;
180     return;
183 /************************************************************************
184  * FUNCTION PURPOSE:  Netapi internal function which returns NWAL handle for a policy
185  ************************************************************************
186  * DESCRIPTION:  Netapi internal function which returns NWAL handle for a policy
187  ************************************************************************/
188 void *netapip_netcpCfgGetPolicy(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
189                                 int policy_slot)
191     if ((policy_slot <0 ) || (policy_slot >= TUNE_NETAPI_MAX_POLICY))
192         return NULL;
193     if (!p->policy[policy_slot].in_use)
194         return NULL;
195     return p->policy[policy_slot].nwal_handle;
198 /********************************************************************
199  * FUNCTION PURPOSE:  Netapi internal function to find a free slot in SA list for a SA
200  ********************************************************************
201  * DESCRIPTION:  Netapi internal function to find a free slot in SA list for an SA
202  ********************************************************************/
203 int netapip_netcpCfgFindSaSlot(NETAPI_HANDLE_T *p_handle,
204                                NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
205                                int iface)
206 {                                    
207    int i;
208    if (iface != NETCP_CFG_NO_INTERFACE)
209    {
210    if ((iface <0 ) || (iface >=TUNE_NETAPI_MAX_NUM_MAC)) return -1;
211    }
212    p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
213    //find a free entry
214    for(i=0;i<TUNE_NETAPI_MAX_SA;i++)
215    {                       
216        if (!p->tunnel[i].in_use)
217        {
218            p->tunnel[i].in_use = 2; //pending
219            p->tunnel[i].iface= iface; //save iface
220            p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
221            return i;
222        }
223    }
224    p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
225    return -1;
228 /********************************************************************
229  * FUNCTION PURPOSE:  Netapi internal function to delete an SA from SA list
230  ********************************************************************
231  * DESCRIPTION: Netapi internal function to delete an SA from SA list 
232  ********************************************************************/
233 void netapip_netcpCfgDeleteSa(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
234                               int sa_slot)
236     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
237     {
238         return ;
239     }
240     p->tunnel[sa_slot].in_use=0;
241     return;
245 /****************************************************************************
246  * FUNCTION PURPOSE:  Internal function to retrieve SB command info template
247  **************************************************************************
248  * DESCRIPTION:  Internal function to retrieve SB command info template
249  ***************************************************************************/
250 nwalTxDmPSCmdInfo_t* netapip_netcpCfgGetSaSBInfo(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
251                                                  NETCP_CFG_SA_T    sa_app_id)
253      int sa_slot = netapi_cfgGetMatchId(sa_app_id);
254     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
255     {
256         return NULL;
257     }
258     return (&p->tunnel[sa_slot].dmPSCmdInfo);
261 /****************************************************************************
262  * FUNCTION PURPOSE:  Internal function to retrieve Inflow mode 
263  *                    software information required to transmit packet
264  **************************************************************************
265  * DESCRIPTION:  Internal function to retrieve Inflow mode 
266  *                    software information required to transmit packet
267  ***************************************************************************/
268 int netapip_netcpCfgGetSaInflowInfo(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
269                                     NETCP_CFG_SA_T    sa_app_id,
270                                     uint32_t *swInfo0,
271                                     uint32_t *swInfo1)
273     int sa_slot = netapi_cfgGetMatchId(sa_app_id);
274     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
275     {
276         return 0;
277     }
278     *swInfo0 = p->tunnel[sa_slot].swInfo0;
279     *swInfo1 = p->tunnel[sa_slot].swInfo1;
280     return 1;
283 /********************************************************************
284  * FUNCTION PURPOSE:  Netapi internal function to insert an SA to the SA list
285  ********************************************************************
286  * DESCRIPTION: Netapi internal function to insert an SA to the SA list 
287  ********************************************************************/
288 void netapip_netcpCfgInsertSa(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
289                               int sa_slot,
290                               int dir,
291                               int mode,
292                               void * temp1,
293                               void * temp2,
294                               void * handle_inflow,
295                               void * handle_sideband,
296                               nwalTxDmPSCmdInfo_t *dmPSCmdInfo,
297                               uint32_t swInfo0,
298                               uint32_t swInfo1,
299                               void* user_data)
301     p->tunnel[sa_slot].in_use=1;
302     p->tunnel[sa_slot].inbound = dir;
303     p->tunnel[sa_slot].sa_mode = mode;
304     p->tunnel[sa_slot].sa_handle_inflow = handle_inflow;
305     p->tunnel[sa_slot].sa_handle_sideband = handle_sideband;
306     p->tunnel[sa_slot].swInfo0 = swInfo0;
307     p->tunnel[sa_slot].swInfo1 = swInfo1;
308     p->tunnel[sa_slot].user_data = user_data;
310     if (dmPSCmdInfo)
311     {
312         memcpy(&p->tunnel[sa_slot].dmPSCmdInfo, dmPSCmdInfo, sizeof(nwalTxDmPSCmdInfo_t));
313     }
314     return;
317 /********************************************************************
318  * FUNCTION PURPOSE:  Netapi internal function to return NWAL handles for an SA
319  ********************************************************************
320  * DESCRIPTION: Netapi internal function to return NWAL handles for an SA
321  ********************************************************************/
322  void *netapip_netcpCfgGetSaHandles(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
323                                     int sa_slot,
324                                     void ** p_sideband)
326     if ((sa_slot <0 ) || (sa_slot >= TUNE_NETAPI_MAX_SA))
327         return NULL;
328     if (!p->tunnel[sa_slot].in_use)
329         return NULL;
330     *p_sideband = p->tunnel[sa_slot].sa_handle_sideband;
331     return p->tunnel[sa_slot].sa_handle_inflow;
335 /*==============================FLOWS=============================*/
337 /********************************************************************
338  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for a flow
339  ********************************************************************
340  * DESCRIPTION: Netapi internal function to find a free slot for a flow
341  ********************************************************************/
342 static int netapip_netcpCfgFindFlowSlot(NETAPI_HANDLE_T *p_handle,
343                                         NETAPI_NWAL_GLOBAL_CONTEXT_T *p)
345     int i;
346     //find a free entry
347     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
348     for(i=0;i<TUNE_NETAPI_MAX_FLOWS;i++)
349     {
350         if (!p->flows[i].in_use)
351         {
352             p->flows[i].in_use = 2; /* pending */
353             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
354             return i;
355         }
356     }
357    p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
358     return -1;
361 /********************************************************************
362  * FUNCTION PURPOSE:  Netapi internal function to find delete a flow from flow list
363  ********************************************************************
364  * DESCRIPTION: Netapi internal function to find delete a flow from flow list
365  ********************************************************************/
366 static void netapip_netcpCfgDeleteFlow(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
367                                        int slot)
369     if ((slot >=0 ) && (slot < TUNE_NETAPI_MAX_FLOWS))
370     {
371         p->flows[slot].in_use = 0;
372     }
375 /********************************************************************
376  * FUNCTION PURPOSE:  Netapi internal function to  insert a flow to the  flow list
377  ********************************************************************
378  * DESCRIPTION: Netapi internal function to  delete a flow from flow list
379  ********************************************************************/
380 static NETCP_CFG_FLOW_HANDLE_T netapip_netcpCfgInsertFlow(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
381                                                           int slot,
382                                                           int dma_engine,
383                                                           void * handle)
385     p->flows[slot].in_use=1;
386     p->flows[slot].handle = handle;
387     p->flows[slot].flow.flowid  = Cppi_getFlowId(handle);
388     p->flows[slot].flow.dma_engine  = dma_engine;
389         return (NETCP_CFG_FLOW_HANDLE_T) &p->flows[slot].flow;
392 /********************************************************************
393  * FUNCTION PURPOSE:  Netapi internal function to find entry matching the flowid
394  ********************************************************************
395  * DESCRIPTION: Netapi internal function to find entry matching the flowid. Returns
396  * the slot number and the cPPI handle.
397  ********************************************************************/
398 static int netapip_netcpCfgFindFlow(NETAPI_HANDLE_T *p_handle,
399                                     NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
400                                     int flowid,  
401                                     int dma_engine,
402                                     void ** handle) 
404     int i;
405     *handle=NULL;
406     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
407     for(i=0;i<TUNE_NETAPI_MAX_FLOWS;i++)
408     {
409        if ((p->flows[i].in_use)&&
410            (p->flows[i].flow.flowid == flowid)&&
411            (p->flows[i].flow.dma_engine == dma_engine))
412         {
413             *handle = p->flows[i].handle;
414              p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
415             return i;
416         }
417     }
418     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
419     return -1;
424 /*============================IP ADDRESSES==========================*/
426 /***************************************************************************
427  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for IP rule in IP slot list
428  ***************************************************************************
429  * DESCRIPTION: NNetapi internal function to find a free slot for IP rule in IP slot list
430  ***************************************************************************/
431 static int netapip_netcpCfgFindIpSlot(NETAPI_HANDLE_T *p_handle,
432                                       NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
433                                       int iface_no)
435     int i;
437     //find a free entry
438     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
439     for(i=0;i<TUNE_NETAPI_MAX_NUM_IP;i++)
440     {
441         if (!p->ips[i].in_use)
442         {
443             p->ips[i].in_use = 2; //pending
444             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
445             return i;
446         }
447     }
448     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
449     return -1;
452 /***************************************************************************
453  * FUNCTION PURPOSE:  Netapi internal function to insert an IP address into interface
454  ***************************************************************************
455  * DESCRIPTION: Netapi internal function to insert an IP address into interface
456  ***************************************************************************/
457 static void netapip_netcpCfgInsertIp(NETAPI_NWAL_GLOBAL_CONTEXT_T *p, 
458                                      nwal_IpType ipType,
459                                      nwalIpAddr_t *ip_addr, 
460                                      nwalIpOpt_t *ip_qualifiers, 
461                                      int iface_no,
462                                      int ip_slot,
463                                      void * handle,
464                                      void * user_data)
466     p->ips[ip_slot].in_use=1;
467     memcpy(&p->ips[ip_slot].ip_addr, ip_addr, sizeof(nwalIpAddr_t));
468     if(ip_qualifiers)
469     {
470         memcpy(&p->ips[ip_slot].ip_qualifiers, ip_qualifiers, sizeof(nwalIpOpt_t));
471     }
472     else
473     {
474         memset(&p->ips[ip_slot].ip_qualifiers, 0, sizeof(nwalIpOpt_t));
475     }
476     p->ips[ip_slot].ip_type = ipType;
477     p->ips[ip_slot].nwal_handle = handle;
478     p->ips[ip_slot].user_data = user_data;
481 /***************************************************************************
482  * FUNCTION PURPOSE:  Netapi internal function to free IP slot associated with IP address
483  ***************************************************************************
484  * DESCRIPTION: Netapi internal function to free IP slot associated with IP address
485  ***************************************************************************/
486 static void netapip_netcpCfgDeleteIp(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
487                                      int iface_no,
488                                      int ip_slot)
490     if ((ip_slot >=0) &&(ip_slot < TUNE_NETAPI_MAX_NUM_IP))
491     {
492         p->ips[ip_slot].in_use=0;
493     }
496 /***************************************************************************
497  * FUNCTION PURPOSE:  Netapi internal function to get IP handle associated with IP address
498  ***************************************************************************
499  * DESCRIPTION: Netapi internal function to get IP handle associated with IP address
500  ***************************************************************************/
501 static void *netapip_netcpCfgGetIpHandle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
502                                          int iface_no,
503                                          int ip_slot)
505     if ((ip_slot <0)||(ip_slot>=TUNE_NETAPI_MAX_NUM_IP))
506         return NULL;
507     if (!p->ips[ip_slot].in_use)
508         return NULL;
509     return (void *) p->ips[ip_slot].nwal_handle;
512 /*==========================MAC INTERFACES======================*/
516 /*******************************************************************************
517  * FUNCTION PURPOSE:  Netapi internal function to insert an interface to the interface list
518  *******************************************************************************
519  * DESCRIPTION:  Netapi internal function to insert an interface to the interface list
520  *******************************************************************************/
521 static void netapip_netcpCfgInsertMac(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
522                                       unsigned char * p_mac,
523                                       int iface_no, 
524                                       int state, NETCP_CFG_VLAN_T vlan, 
525                                       void * handle)
527     if ((iface_no >=0 ) && (iface_no < TUNE_NETAPI_MAX_NUM_MAC))
528     {
529         memset(&p->interfaces[iface_no],0,sizeof(NETCP_INTERFACE_T));
530         p->interfaces[iface_no].in_use = 1;
531         memcpy(&p->interfaces[iface_no].mac[0], p_mac,6);
532         p->interfaces[iface_no].state = state;
533         //todo p->interfaces[iface_no].vlan = vlan;
534         p->interfaces[iface_no].nwal_handle = handle; //save handle assoicated with this rule
536     }
539 /*******************************************************************************
540  * FUNCTION PURPOSE:  Netapi internal function to get handle associated with interface
541  *******************************************************************************
542  * DESCRIPTION:  Netapi internal function to get handle associated with interface
543  *******************************************************************************/
544 void* netapip_netcpCfgGetMacHandle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
545                                    int iface_no)
547     if (iface_no == NETCP_CFG_NO_INTERFACE)
548     {
549         return NULL;
550     }
551     if ((iface_no <0 ) || (iface_no >= TUNE_NETAPI_MAX_NUM_MAC))
552     {
553         return NULL;
554     }
555     else if ( p->interfaces[iface_no].in_use)
556     {
557       return (void *) p->interfaces[iface_no].nwal_handle;
558     }
559     //no valid entry in slot
560     return NULL;
562 /*******************************************************************************
563  * FUNCTION PURPOSE:  Netapi internal function to delete interface from interface list
564  *******************************************************************************
565  * DESCRIPTION:  Netapi internal function to delete interface from interface list
566  *******************************************************************************/
567 static void netapip_netcpCfgDeleteMac(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
568                                       int iface_no)
570     if ((iface_no >=0 ) && (iface_no < TUNE_NETAPI_MAX_NUM_MAC))
571     {
572         p->interfaces[iface_no].in_use = 0;
573     }
576 /*========================CLASSIFIERS==========================*/
578 /*******************************************************************************
579  * FUNCTION PURPOSE:  Netapi internal function to find a free slot for classifier rule
580  *******************************************************************************
581  * DESCRIPTION:  Netapi internal function to find a free slot for classifier rule
582  *******************************************************************************/
583 static int netapip_netcpCfgFindClassSlot(NETAPI_HANDLE_T *p_handle,
584                                          NETAPI_NWAL_GLOBAL_CONTEXT_T *p)
586     int i;
587     p_handle->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_lock);
588     for(i=0;i<TUNE_NETAPI_MAX_CLASSIFIERS;i++)
589     {
590         if (!p->classi[i].in_use)
591         {
592             p->classi[i].in_use = 2; //pending
593             p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
594             return i;
595         }
596     }
597     p_handle->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_lock);
598     return -1;
600 /*******************************************************************************
601  * FUNCTION PURPOSE:  Netapi internal function to find a delete a classifer from classifer list
602  *******************************************************************************
603  * DESCRIPTION:  Netapi internal function to find a delete a classifer from classifer list
604  *******************************************************************************/
605 static void netapip_netcpCfgDeleteClass(
606          NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
607          int class_slot )
609     if ((class_slot <0 ) || (class_slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
610     {
611         return;
612     }
613     p->classi[class_slot].in_use=0;
616 /*******************************************************************************
617  * FUNCTION PURPOSE:  Netapi internal function to find a insert a classifer to classifer list
618  *******************************************************************************
619  * DESCRIPTION:  Netapi internal function to find a insert a classifer to classifer list
620  *******************************************************************************/
621 static void netapip_netcpCfgInsertClass(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
622                                         int class_slot,
623                                         int class_type,
624                                         void * L2_handle,
625                                         void * L3_handle,
626                                         void * L4_handle,
627                                         void * user_data)
629     if ((class_slot >=0 ) && (class_slot < TUNE_NETAPI_MAX_CLASSIFIERS))
630     {
631         p->classi[class_slot].in_use=1;
632         p->classi[class_slot].nwal_L2_handle = L2_handle;
633         p->classi[class_slot].nwal_L3_handle = L3_handle;
634         p->classi[class_slot].nwal_L4_handle = L4_handle;
635         p->classi[class_slot].class_type = class_type;
636         p->classi[class_slot].user_data = user_data;
637     }
640 /*******************************************************************************
641  * FUNCTION PURPOSE:  Netapi internal function to get L4 nwal handle for classifier
642  *******************************************************************************
643  * DESCRIPTION:  Netapi internal function to get L4 nwal handle for classifier
644  *******************************************************************************/
645 static void *netapip_netcpCfgGetL4Handle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
646                                          int class_slot)
648     if ((class_slot <0 ) || (class_slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
649     {
650         return NULL;
651     }
652     if (!p->classi[class_slot].in_use)
653     {
654         return NULL;
655     }
656     return p->classi[class_slot].nwal_L4_handle;
661 /*******************************************************************************
662  * FUNCTION PURPOSE:  Netapi internal function to get L3 nwal handle for classifier
663  *******************************************************************************
664  * DESCRIPTION:  Netapi internal function to get L3 nwal handle for classifier
665  *******************************************************************************/
666 static int cfgOkToDeleteL3ClassiHandle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
667                                          void*  l3_handle)
669     int i;
670     int count=0;
672     if(l3_handle == NULL)
673         return NETAPI_FALSE;
674     for(i=0; i< TUNE_NETAPI_MAX_CLASSIFIERS;i++)
675     {
676         if (!p->classi[i].in_use)
677         {
678             continue;
679         }
680         if (l3_handle == p->classi[i].nwal_L3_handle)
681         {
682             count++;
683         }
684     }
685     if (count > 0)
686         return NETAPI_FALSE;
688     return NETAPI_TRUE;
691 /*******************************************************************************
692  * FUNCTION PURPOSE:  Netapi internal function to get L3 nwal handle for classifier
693  *******************************************************************************
694  * DESCRIPTION:  Netapi internal function to get L3 nwal handle for classifier
695  *******************************************************************************/
696 static void *netapip_netcpCfgGetL3Handle(NETAPI_NWAL_GLOBAL_CONTEXT_T *p,
697                                          int class_slot)
699     if ((class_slot <0 ) || (class_slot >= TUNE_NETAPI_MAX_CLASSIFIERS))
700     {
701         return NULL;
702     }
703     if (!p->classi[class_slot].in_use)
704     {
705         return NULL;
706     }
707     return p->classi[class_slot].nwal_L3_handle;
711 /***********************************************************************************/
712 /****************************************API****************************************/
713 /***********************************************************************************/
716 /********************************************************************
717  * FUNCTION PURPOSE:  API to request statistics from NETCP
718  ********************************************************************
719  * DESCRIPTION:  API to request statistics from NETCP
720  ********************************************************************/
721 void netapi_netcpCfgReqStats(NETAPI_T  h, 
722                              NETCP_CFG_STATS_CB cb,
723                              int doClear,
724                              int *err)
726     nwal_RetValue ret;
727     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
728     NetapiNwalTransInfo_t *pTransInfo;
729     nwal_TransID_t     transId;
731     paSysStats_t netcp_stats;
732     if ((!n) || (!cb))
733     {
734         *err = NETAPI_ERR_BAD_INPUT;
735         return ;
736     }
737     *err =0;
739     pTransInfo = netapip_getFreeTransInfo(n,
740                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
741                                           &transId);
742     if (!pTransInfo) { *err =  NETAPI_ERR_BUSY; return ;}
743     pTransInfo->transType = NETAPI_NWAL_HANDLE_STAT_REQUEST;
744     pTransInfo->netapi_handle = h;
745     n->nwal_local.stats_cb = cb;
747     memset(&netcp_stats, 0, sizeof(paSysStats_t));
748     ret = nwal_getPAStats( ((NETAPI_GLOBAL_T *) n->global)->nwal_context.nwalInstHandle,
749                               transId,
750                               &netcp_stats,
751                               doClear);
752     if(ret !=  nwal_OK)
753     {
754         *err = NETAPI_ERR_BUSY;  //no resources??
755         netapip_freeTransInfo(pTransInfo);
756         return;
757     }
758 #if defined(DEVICE_K2E) || defined(DEVICE_K2L)
759     if (cb)
760     {
761         cb(h, &netcp_stats);
762     }
763 #endif
764     netapip_freeTransInfo(pTransInfo);
766 /********************************************************************
767  * FUNCTION PURPOSE:  API to create a MAC interface
768  ********************************************************************
769  * DESCRIPTION:  API to create a MAC interface
770  ********************************************************************/
771 NETCP_CFG_MACIF_T  netapi_netcpCfgCreateMacInterface(NETAPI_T  h,
772                                                      uint8_t *p_mac,
773                                                      uint8_t *p_mac_remote,
774                                                      int  iface_no, /* index, from 0 to n */
775                                                      int switch_port,
776                                                      NETCP_CFG_ROUTE_HANDLE_T  route,
777                                                      NETCP_CFG_VLAN_T  vlan,  //future
778                                                      uint16_t etherType,
779                                                      int state,  //0=down, 1=up  //ignored
780                                                      int * err)
782     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
783     nwalMacParam_t   macInfo=
784     {
785         0,      /* validParams */
786         0,      /* ifNum */
787         0,      /* vlanId      */
788         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      /* Local mac */
789         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },      /* Remote mac */
790         NWAL_MATCH_ACTION_CONTINUE_NEXT_ROUTE,       /* Continue parsing to next route for match */
791         NWAL_NEXT_ROUTE_FAIL_ACTION_HOST,            /* For next route fail action by default is route to host */
792         CPPI_PARAM_NOT_SPECIFIED,                    /* Use default flow configured to NWAL  if packet is routed to host */
793         QMSS_PARAM_NOT_SPECIFIED,                     /* Use default queue configured to NWAL if packet is routed to host */
794         0,
795         0,
796         0
797     };
799     nwal_RetValue       retValue;
800     NetapiNwalTransInfo_t *pTransInfo;
801     nwal_TransID_t     trans_id;
802     nwal_AppId         appId;
803     if ((!n) || (!p_mac)) {*err = NETAPI_ERR_BAD_INPUT; return -1;}
804     *err =0;
806     pTransInfo = netapip_getFreeTransInfo(n,
807                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
808                                           &trans_id);
809     if (!pTransInfo)
810     {
811         *err =  NETAPI_ERR_BUSY;
812         return -1;
813     }
814     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_MAC;
815     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING;
816     pTransInfo->inUse = nwal_TRUE;
817     pTransInfo->netapi_handle = h;
819     /* set up MacInfo */
820     memcpy(&macInfo.macAddr,p_mac,6);
822     if(p_mac_remote)
823     {
824         macInfo.validParams |=  NWAL_SET_MAC_VALID_PARAM_REMOTE_MAC;
825         memcpy(&macInfo.remMacAddr,p_mac_remote,6);
826     }
827     /* todo: vlan */
828     if (switch_port)
829     {
830         macInfo.validParams |= NWAL_SET_MAC_VALID_PARAM_IFNUM ;
831         macInfo.ifNum = switch_port;
832     }
834     if(etherType)
835     {
836         macInfo.validParams |= NWAL_SET_MAC_VALID_PARAM_ETHER_TYPE;
837         macInfo.etherType = etherType;
838     }
840     if (route != NULL)
841     {
842         if((route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
843             NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
844         {
845            macInfo.validParams |= 
846                     NWAL_SET_MAC_VALID_PARAM_ROUTE_TYPE;
848            if(route->routeType == NWAL_ROUTE_PKTTYPE_EQOS)
849            {
850                 macInfo.egress_switch_port = route->egress_swith_port;
851                 //macInfo.appRxPktFlowId = route->p_flow;
852                 //macInfo.appRxPktQueue = route->p_dest_q;
853                 macInfo.routeType = NWAL_ROUTE_PKTTYPE_EQOS;
854                 macInfo.matchAction = NWAL_MATCH_ACTION_HOST;
855            }
856         }
858         netapip_netcpCfgBuildRoute(route,
859                                    &macInfo.appRxPktFlowId,
860                                    &macInfo.appRxPktQueue,
861                                    &macInfo.routeType);
862     }
865     appId = (nwal_AppId) (NETAPI_NETCP_MATCH_GENERIC_MAC | iface_no);
867     retValue = nwal_setMacIface( ((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
868                                   trans_id,
869                                   appId,
870                                   &macInfo,
871                                   &pTransInfo->handle);
872     if(retValue !=  nwal_OK)
873     {
874         *err = NETAPI_ERR_NWAL_ERR0;
875         netapip_freeTransInfo(pTransInfo);
876         return -1;
877     }
879     //wait here until its done since scheduler isn't running yet most likely..
880     // todo:  make this handled by scheduler poll later ??
881     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
882     {
883         n->nwal_local.numPendingCfg++;
884         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
885                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_OPEN))
886         {
887             // if response is there, then this poll squirts out in the CTl poll callback, 
888             // which handles the rest (including decrmenting #pending!!
889             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
890         }
891         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
892         {
893             netapip_freeTransInfo(pTransInfo);
894             *err = NETAPI_ERR_PA_FW;
895             return -1;
896         }
898     }
899     netapip_netcpCfgInsertMac(&netapi_get_global()->nwal_context, 
900                           p_mac, iface_no, state,vlan,
901                           (void *) pTransInfo->handle);
902     netapip_freeTransInfo(pTransInfo);
903     return (NETCP_CFG_MACIF_T)appId;
906 /********************************************************************
907  * FUNCTION PURPOSE:  API to delete MAC  interface
908  ********************************************************************
909  * DESCRIPTION:  API to delete MAC  interface
910  ********************************************************************/
911 void netapi_netcpCfgDelMac(NETAPI_T h,
912                            int iface_no,
913                            int *err)
915     nwal_RetValue ret;
916     NetapiNwalTransInfo_t *pTransInfo;
917     nwal_TransID_t     trans_id;
918     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
919     void * ifHandle;
921     //get the nwal handle assoicated with this iface
922     ifHandle = netapip_netcpCfgGetMacHandle(&netapi_get_global()->nwal_context, iface_no );
923     if(!ifHandle) 
924     {
925         *err = NETAPI_ERR_BAD_INPUT; return ;
926     }
927     *err =0;
929     //get a transaction id
930     pTransInfo = netapip_getFreeTransInfo(n,
931                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
932                                           &trans_id);
933     if (!pTransInfo)
934     {
935         *err =  NETAPI_ERR_BUSY;
936         return ;
937     }
938     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_MAC;
939     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING;
940     pTransInfo->inUse = nwal_TRUE;
941     pTransInfo->netapi_handle = h;
943     //issue request
944     ret = nwal_delMacIface(
945                 ((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
946                 trans_id,
947                 ifHandle);
948     if(ret !=  nwal_OK)
949     {
950         *err = NETAPI_ERR_NWAL_ERR0;
951         netapip_freeTransInfo(pTransInfo);
952         return;
953     }
954     //wait here until its done since scheduler isn't running yet most likely..
955     // todo:  make this handled by scheduler poll later ??
956     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
957     {
958         n->nwal_local.numPendingCfg++;
959         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
960                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_IDLE))
961         {
962             // if response is there, then this poll squirts out in the CTl poll callback, 
963             // which handles the rest (including decrmenting #pending!!
964             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
965         }
966         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
967         {
968             netapip_freeTransInfo(pTransInfo);
969             *err = NETAPI_ERR_PA_FW;
970             netapip_netcpCfgDeleteMac(&netapi_get_global()->nwal_context,  iface_no);
971             return;
972         }
973     }
974     netapip_freeTransInfo(pTransInfo);
975     //zap the entry
976     netapip_netcpCfgDeleteMac(&netapi_get_global()->nwal_context,  iface_no);
977     return ;
981 /********************************************************************
982  * FUNCTION PURPOSE:  NETAPi internal function to Add IP to MAC interface
983  ********************************************************************
984  * DESCRIPTION:  NETAPi internal function to Add IP to MAC interface
985  ********************************************************************/
986 static NETCP_CFG_IP_T  netapip_netcpCfgAddIpInternal(NETAPI_T  h,
987                                                      int  iface_no,
988                                                      nwal_IpType ipType,
989                                                      nwalIpAddr_t  * ip_addr,
990                                                      nwalIpAddr_t  *ip_rem_addr,
991                                                      nwalIpOpt_t * ip_qualifiers,
992                                                      NETCP_CFG_ROUTE_HANDLE_T  route,/*NULL for default*/
993                                                      void * user_data,
994                                                      int * err,
995                                                      int  flag) /*TRUE: add IP to iface,
996                                                      FALSE: add IP as part of classifier */
998     static unsigned char zeroIP6[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
999     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1000     void * n_handle=NULL;
1001     nwalIpParam_t    nwalIpParam= {
1002     0,
1003     pa_IPV4,      /* IP Type */
1004     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Dest IP */
1005     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Source IP */
1006     { 0x0,0,0,0},/* IP Options */
1007     NWAL_MATCH_ACTION_CONTINUE_NEXT_ROUTE,       /* Continue parsing to next route for match */
1008     NWAL_NEXT_ROUTE_FAIL_ACTION_HOST,            /* For next route fail action by default is route to host */
1009     CPPI_PARAM_NOT_SPECIFIED,                    /* Use default flow configured to NWAL  if packet is routed to host */
1010     QMSS_PARAM_NOT_SPECIFIED,                     /* Use default queue configured to NWAL if packet is routed to host */
1011     0
1012 };
1013     nwal_RetValue       retValue;
1014     NetapiNwalTransInfo_t *pTransInfo;
1015     nwal_TransID_t     trans_id;
1016     int ip_slot=-1;
1017     NETCP_CFG_IP_T ip_rule_id;
1018     NETCP_CFG_IP_T temp;
1020     //verify that iface has been configured 
1021     if (iface_no != NETCP_CFG_NO_INTERFACE)
1022     {
1023         if ((iface_no<0) || (iface_no>= TUNE_NETAPI_MAX_NUM_MAC)) {*err = NETAPI_ERR_BAD_INPUT; return -1;}
1024     }
1026     if (iface_no != NETCP_CFG_NO_INTERFACE)
1027     {
1028         if(netapi_get_global()->nwal_context.interfaces[iface_no].in_use)
1029         {
1030             n_handle =  netapi_get_global()->nwal_context.interfaces[iface_no].nwal_handle;
1031         }
1032         else
1033         {
1034             *err = NETAPI_ERR_BAD_INPUT;
1035             return -1;
1036         }
1037     }
1038     if (flag) //if adding IP to MAC then reserve a slot to save info
1039     {
1040         //find free slot for IP & reserve
1041          ip_slot= netapip_netcpCfgFindIpSlot(n,
1042                                              &netapi_get_global()->nwal_context, 
1043                                              iface_no);
1044         if (ip_slot <0) 
1045         {
1046             *err= NETAPI_ERR_NOMEM;  //no room 
1047             return -1;
1048         }
1049     }
1051     //get a transaction object for config action
1052     pTransInfo = netapip_getFreeTransInfo(n, 
1053                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1054                                           &trans_id);
1055     if (!pTransInfo)
1056     {
1057         *err =  NETAPI_ERR_BUSY;
1058         if (flag)
1059         {
1060             netapip_netcpCfgDeleteIp(&netapi_get_global()->nwal_context,
1061                          iface_no,
1062                          ip_slot);
1063         }
1064         return -1;
1065     }
1066     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_IP;
1067     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING;
1068     pTransInfo->inUse = nwal_TRUE;
1069     pTransInfo->netapi_handle = h;
1071     //build nwalIpParam
1072     memcpy(&nwalIpParam.locIpAddr,ip_addr, sizeof(nwalIpAddr_t));
1073     if (ip_rem_addr)
1074     {
1075         if (ipType == nwal_IPV4)
1076         {
1077             if(memcmp(ip_rem_addr, zeroIP6, sizeof(nwalIpv4Addr_t)))
1078             {
1079                 nwalIpParam.validParams |= NWAL_SET_IP_VALID_PARAM_REMOTE_IP;
1080                 memcpy(&nwalIpParam.remIpAddr,ip_rem_addr, sizeof(nwalIpAddr_t));
1081             }
1082         }
1083         else
1084         {
1085             if(memcmp(ip_rem_addr, zeroIP6, sizeof(nwalIpv6Addr_t)))
1086             {
1087                 nwalIpParam.validParams |= NWAL_SET_IP_VALID_PARAM_REMOTE_IP;
1088                 memcpy(&nwalIpParam.remIpAddr,ip_rem_addr, sizeof(nwalIpAddr_t));
1089             }
1090         }
1091     }
1092     nwalIpParam.ipType=ipType;
1093     if(route)
1094     {
1095         if((route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
1096             NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
1097         {
1098            nwalIpParam.validParams |= 
1099                     NWAL_SET_IP_VALID_PARAM_ROUTE_TYPE;
1100         }
1101         netapip_netcpCfgBuildRoute(route,
1102                                    &nwalIpParam.appRxPktFlowId,
1103                                    &nwalIpParam.appRxPktQueue,
1104                                    &nwalIpParam.routeType);
1105     }
1106     else{} //use nwal defaults
1108     if (ip_qualifiers)
1109         memcpy(&nwalIpParam.ipOpt,ip_qualifiers, sizeof(nwalIpOpt_t)); 
1110     else
1111         memset(&nwalIpParam.ipOpt,0, sizeof(nwalIpOpt_t));
1113     //build the rule id that will be returned when a packet matches 
1114     if (flag)
1115         ip_rule_id = NETAPI_NETCP_MATCH_GENERIC_IP | iface_no | ((ip_slot&& NETAPI_NETCP_MATCH_ID_MASK) << NETAPI_NETCP_MATCH_ID_SHIFT);
1116     else
1117         ip_rule_id = (NETAPI_NETCP_MATCH_CLASS_L3 | iface_no);
1119     retValue = nwal_setIPAddr(   netapi_get_global()->nwal_context.nwalInstHandle,
1120                                   trans_id,
1121                                   (nwal_AppId) (ip_rule_id),
1122                                   n_handle,
1123                                   &nwalIpParam,
1124                                   &pTransInfo->handle);
1126     if(retValue !=  nwal_OK)
1127     {
1128         *err = NETAPI_ERR_NWAL_ERR0;
1129         netapip_freeTransInfo(pTransInfo);
1130         //zap the entry
1131         if (flag)
1132         {
1133             netapip_netcpCfgDeleteIp(&netapi_get_global()->nwal_context,
1134                          iface_no,
1135                          ip_slot);
1136         }
1137         return -1;
1138     }
1139     //wait here until its done since scheduler isn't running yet most likely..
1140     // todo:  make this handled by scheduler poll later ??
1141     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1142     {
1143         n->nwal_local.numPendingCfg++;
1144         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1145         (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_OPEN))
1146         {
1147             // if response is there, then this poll squirts out in the CTl poll callback, 
1148             // which handles the rest (including decrmenting #pending!!
1149             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1150         }
1151         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1152         {
1153             netapip_freeTransInfo(pTransInfo);
1154             *err = NETAPI_ERR_PA_FW;
1155             return -1;
1156         }
1157     }
1158     if (flag)
1159     {
1160         netapip_netcpCfgInsertIp(&netapi_get_global()->nwal_context,
1161                                  ipType, 
1162                                  ip_addr,
1163                                  ip_qualifiers,
1164                                  iface_no,
1165                                  ip_slot,
1166                                  pTransInfo->handle,
1167                                  user_data);
1168     }
1170     temp = (NETCP_CFG_IP_T) pTransInfo->handle;
1171     netapip_freeTransInfo(pTransInfo);
1172     return  (flag ? ip_rule_id:  temp);
1175 /********************************************************************
1176  * FUNCTION PURPOSE:  API to Add IP to MAC interface
1177  ********************************************************************
1178  * DESCRIPTION:  API to Add IP to MAC interface
1179  ********************************************************************/
1180 NETCP_CFG_IP_T  netapi_netcpCfgAddIp(NETAPI_T                  h,
1181                                      int                       iface_no,
1182                                      nwal_IpType               ipType,
1183                                      nwalIpAddr_t*             ip_addr,
1184                                      nwalIpAddr_t*             ip_rem_addr,
1185                                      nwalIpOpt_t*              ip_qualifiers,
1186                                      NETCP_CFG_ROUTE_HANDLE_T  route,  //NULL for default
1187                                      void*                     user_data,
1188                                      int*                      err)
1190     return netapip_netcpCfgAddIpInternal(h,
1191                                          iface_no,
1192                                          ipType,
1193                                          ip_addr,
1194                                          ip_rem_addr,
1195                                          ip_qualifiers, 
1196                                          route, 
1197                                          user_data,
1198                                          err,
1199                                          1);
1203 /********************************************************************
1204  * FUNCTION PURPOSE:  NETAPI internal function to detach IP from MAC interface
1205  ********************************************************************
1206  * DESCRIPTION:  NETAPI internal function to detach IP from MAC interface
1207  ********************************************************************/
1208 static void netapip_netcpCfgDelIpInternal(NETAPI_T h, 
1209                                           int iface_no,
1210                                           nwal_IpType ipType,
1211                                           nwalIpAddr_t  * ip_addr,
1212                                           nwalIpOpt_t * ip_qualifiers, 
1213                                           NETCP_CFG_IP_T  ip_rule_id,
1214                                           int *err, 
1215                                           void * handle,   /* if flag==0, handle must be valid */
1216                                           int flag)        /* flag==0 => delete IP rule that was part of classifier, not interface */
1218     nwal_RetValue ret;
1219     NetapiNwalTransInfo_t *pTransInfo;
1220     nwal_TransID_t     trans_id;
1221     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1222     void * ifHandle;
1223     int ip_slot = netapi_cfgGetMatchId(ip_rule_id);
1224     /* get the nwal handle assoicated with this ip */
1225     if (flag)
1226     {
1227         ifHandle = netapip_netcpCfgGetIpHandle(&netapi_get_global()->nwal_context,
1228                                                 iface_no,
1229                                                 ip_slot);
1230     }
1231     else 
1232     {
1233         ifHandle = handle;
1234     }
1235     if(!ifHandle)
1236     {
1237         *err = NETAPI_ERR_BAD_INPUT;
1238         return ;
1239     }
1240     *err =0;
1242     //get a transaction id
1243     pTransInfo = netapip_getFreeTransInfo(n,
1244                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1245                                           &trans_id);
1246     if (!pTransInfo)
1247     {
1248         *err =  NETAPI_ERR_BUSY;
1249         return;
1250     }
1251     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_IP;
1252     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING;
1253     pTransInfo->inUse = nwal_TRUE;
1254     pTransInfo->netapi_handle = h;
1255     //issue request
1256     ret = nwal_delIPAddr(
1257                 ((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1258                 trans_id,
1259                 ifHandle);
1260     if(ret !=  nwal_OK)
1261     {
1262         *err = NETAPI_ERR_NWAL_ERR0;
1263         netapip_freeTransInfo(pTransInfo);
1264         return ;
1265     }
1266     //wait here until its done since scheduler isn't running yet most likely..
1267     // todo:  make this handled by scheduler poll later ??
1268     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1269     {
1270         n->nwal_local.numPendingCfg++;
1271         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1272                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_IDLE))
1273         {
1274             // if response is there, then this poll squirts out in the CTl poll callback, 
1275             // which handles the rest (including decrmenting #pending!!
1276             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1277         }
1278         if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1279         {
1280             netapip_freeTransInfo(pTransInfo);
1281             *err = NETAPI_ERR_PA_FW;
1282             return;
1283         }
1284     }
1286     netapip_freeTransInfo(pTransInfo);
1288     //zap the entry
1289     if (flag)
1290         netapip_netcpCfgDeleteIp(&netapi_get_global()->nwal_context,  
1291                          iface_no,
1292                          ip_slot);
1293     return ;
1296 /********************************************************************
1297  * FUNCTION PURPOSE:  API to detach IP from MAC interface
1298  ********************************************************************
1299  * DESCRIPTION:  API  to detach IP from MAC interface
1300  ********************************************************************/
1301 void netapi_netcpCfgDelIp(NETAPI_T              h,
1302                           int                   iface_no,
1303                           nwal_IpType           ipType,
1304                           nwalIpAddr_t*         ip_addr,
1305                           nwalIpOpt_t*          ip_qualifiers,
1306                           NETCP_CFG_IP_T        ip_rule_id,
1307                           int*                  err)
1309     netapip_netcpCfgDelIpInternal( h, iface_no, ipType,
1310                   ip_addr, ip_qualifiers, ip_rule_id,
1311                   err, NULL, 1);
1312     return;
1316 /********************************************************************
1317  * FUNCTION PURPOSE:  API to attach a classifier rule to NETCP
1318  ********************************************************************
1319  * DESCRIPTION:  API to attach a classifier rule to NETCP
1320  ********************************************************************/
1321 NETCP_CFG_CLASS_T netapi_netcpCfgAddClass(NETAPI_T                  h,
1322                                           NETCP_CFG_CLASSIFIER_T*   p_class,
1323                                           NETCP_CFG_ROUTE_HANDLE_T  route,
1324                                           int                       action, 
1325                                           void*                     user_data, 
1326                                           int*                      err)
1328     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1329     void * l3_handle=NULL;  //ip handle
1330     nwal_RetValue       retValue;
1331     NetapiNwalTransInfo_t *pTransInfo;
1332     nwal_TransID_t     trans_id;
1333     int class_slot=-1;
1334     int iface_no;
1335     int ip_slot=-1;
1336     NETCP_CFG_CLASS_T  classHandle;  //returned by us
1337     nwal_appProtoType_t proto;
1338     nwalRxConnCfg_t tempCfg=
1339     {
1340         0,  //nwal_handle: to be filled in
1341         0, // l4 ports: to be filled in
1342         0,  //core id (NA)
1343         0, //action
1344         CPPI_PARAM_NOT_SPECIFIED, //flow id
1345         QMSS_PARAM_NOT_SPECIFIED, //dest queue
1346         0,
1347         0, // l4 ports: to be filled in
1348     };
1350     *err = NETAPI_ERR_OK;
1352     if(!p_class)
1353     {
1354         *err=NETAPI_ERR_BAD_INPUT;
1355         return -1;
1356     }
1357     switch(p_class->classType)
1358     {
1359         default:
1360             break;
1361         case(NETCP_CFG_CLASS_TYPE_L3_L4):
1362             n->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1363         case(NETCP_CFG_CLASS_TYPE_L4):
1364             //assume just type l4 only (L2, L3 defined by iface, l3 id )
1365             iface_no = p_class->u.c_l4.iface;
1366             if (p_class->classType== NETCP_CFG_CLASS_TYPE_L4)
1367             {
1368                 ip_slot = netapi_cfgGetMatchId(p_class->u.c_l4.ip);
1369             }
1371             //verify that iface has been configured 
1372             if (iface_no != NETCP_CFG_NO_INTERFACE)
1373             {
1374                 if(!netapi_get_global()->nwal_context.interfaces[iface_no].in_use)
1375                 {
1376                     *err = NETAPI_ERR_BAD_INPUT;
1377                     if(p_class->classType== NETCP_CFG_CLASS_TYPE_L3_L4)
1378                         n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1379                     return -1;
1380                 }
1381             }
1383             if (p_class->classType== NETCP_CFG_CLASS_TYPE_L4)
1384             {
1385                 //verify that ip has been configured and get its handle
1386                 l3_handle = netapip_netcpCfgGetIpHandle(
1387                       &netapi_get_global()->nwal_context, iface_no,
1388                       ip_slot );
1389             }
1390             else
1391             {
1392                 nwalIpParam_t tempParam=
1393                 {
1394                     0,
1395                     pa_IPV4,      /* IP Type */
1396                     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Dest IP */
1397                     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },  /* Source IP */
1398                     { 0x0,0,0,0},/* IP Options */
1399                     NWAL_MATCH_ACTION_CONTINUE_NEXT_ROUTE,       /* Continue parsing to next route for match */
1400                     NWAL_NEXT_ROUTE_FAIL_ACTION_HOST,            /* For next route fail action by default is route to host */
1401                     CPPI_PARAM_NOT_SPECIFIED,                    /* Use default flow configured to NWAL  if packet is routed to host */
1402                     QMSS_PARAM_NOT_SPECIFIED,                    /* Use default queue configured to NWAL if packet is routed to host */
1403                     0
1404                 };
1405                 //build nwalIpParam
1406                 memcpy(&tempParam.locIpAddr,p_class->u.c_l3_l4.ip_addr, sizeof(nwalIpAddr_t));
1407                 tempParam.ipType=p_class->u.c_l3_l4.ipType;
1408                 //use nwal defauls for route
1409                 if (p_class->u.c_l3_l4.ip_qualifiers)
1410                     memcpy(&tempParam.ipOpt,p_class->u.c_l3_l4.ip_qualifiers, sizeof(nwalIpOpt_t));
1411                 else
1412                     memset(&tempParam.ipOpt,0, sizeof(nwalIpOpt_t));
1414                 //find if we have a matching L3 handle for IP classifier; if not create it
1415                 retValue =  nwal_getIPAddr (((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1416                             &tempParam,
1417                             netapip_netcpCfgGetMacHandle(&netapi_get_global()->nwal_context, iface_no ),
1418                             &l3_handle); 
1419                 if (retValue != nwal_TRUE) 
1420                 {
1421                     int ret;
1422                     //**NEW IP RULE  
1423                     //need to attach this IP RULE to the MAC
1424                     l3_handle= (void *) netapip_netcpCfgAddIpInternal(
1425                                       h, iface_no, 
1426                                       p_class->u.c_l3_l4.ipType,
1427                                       p_class->u.c_l3_l4.ip_addr,
1428                                       NULL,
1429                                       p_class->u.c_l3_l4.ip_qualifiers,
1430                                       p_class->u.c_l3_l4.p_fail_route,
1431                                       user_data,
1432                                       &ret,
1433                                       NETAPI_FALSE);
1434                     if(!ret)
1435                     {
1436                         l3_handle=NULL;
1437                     }
1438                 }
1439             }
1440             if(!l3_handle)
1441             {
1442                 *err = NETAPI_ERR_BAD_INPUT; 
1443                 if(p_class->classType == NETCP_CFG_CLASS_TYPE_L3_L4)
1444                         n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1445                 return -1 ;
1446             }
1448             //find free slot for CLASS & reserve
1449             class_slot= netapip_netcpCfgFindClassSlot(n,
1450                                                       &netapi_get_global()->nwal_context);
1451             if(class_slot<0) 
1452             {
1453                 *err = NETAPI_ERR_NOMEM;
1454                 if(p_class->classType == NETCP_CFG_CLASS_TYPE_L3_L4)
1455                         n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1456                 return -1;
1457                 }
1458             classHandle = NETAPI_NETCP_MATCH_CLASS |
1459                           (class_slot << NETAPI_NETCP_MATCH_ID_SHIFT) | 
1460                           (iface_no & NETAPI_NETCP_MATCH_LOGICAL_MAC_IFACE_MASK);
1461             //build request from template
1462             tempCfg.inHandle=l3_handle;
1463             if (p_class->classType== NETCP_CFG_CLASS_TYPE_L4)
1464             {
1465                 memcpy(&tempCfg.appProto,&p_class->u.c_l4.appProto,sizeof(nwalAppProto_t));
1466                 proto = p_class->u.c_l4.proto;
1467             }
1468             else
1469             {
1470                 memcpy(&tempCfg.appProto,&p_class->u.c_l3_l4.appProto,sizeof(nwalAppProto_t));
1471                 proto = p_class->u.c_l3_l4.proto;
1472             }
1474             tempCfg.matchAction = (action==NETCP_CFG_ACTION_TO_SW)  ? NWAL_MATCH_ACTION_HOST : NWAL_MATCH_ACTION_DISCARD;
1475             if (route)
1476             {
1477                 if((route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
1478                     NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
1479                 {
1480                     tempCfg.validParams |= 
1481                         NWAL_SET_CONN_VALID_PARAM_ROUTE_TYPE;
1482                 }
1483                 netapip_netcpCfgBuildRoute(route,
1484                                            &tempCfg.appRxPktFlowId,
1485                                            &tempCfg.appRxPktQueue,
1486                                            &tempCfg.routeType);
1487             }
1490             //get a transaction id
1491             pTransInfo = netapip_getFreeTransInfo(n,
1492                                                   (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1493                                                   &trans_id);
1494             if (!pTransInfo)
1495             {
1496                 *err =  NETAPI_ERR_BUSY;
1497                 if(p_class->classType == NETCP_CFG_CLASS_TYPE_L3_L4)
1498                     n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1499                 return -1;
1500             }
1501             pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_PORT;
1502             pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING;
1503             pTransInfo->inUse = nwal_TRUE;
1504             pTransInfo->netapi_handle = h;
1505                 //issue request
1506             retValue = nwal_addConn(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1507                             trans_id,
1508                             (nwal_AppId) classHandle,
1509                             proto,
1510                             &tempCfg,
1511                             NULL,
1512                             &pTransInfo->handle);
1513             if(retValue !=  nwal_OK)
1514             {
1515                 *err = NETAPI_ERR_NWAL_ERR0;
1516                 netapip_freeTransInfo(pTransInfo);
1517                 netapip_netcpCfgDeleteClass(&netapi_get_global()->nwal_context, class_slot);
1518                 if(p_class->classType== NETCP_CFG_CLASS_TYPE_L3_L4)
1519                     n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1520                 return -1;
1521             }
1522             //wait here until its done since scheduler isn't running yet most likely..
1523            // todo:  make this handled by scheduler poll later ??
1524             if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1525             {
1526                 n->nwal_local.numPendingCfg++;
1527                 while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1528                     (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_OPEN))
1529                 {
1530                     // if response is there, then this poll squirts out in the CTl poll callback, 
1531                     // which handles the rest (including decrmenting #pending!!
1532                     nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1533                 }
1534                 if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1535                 {
1536                     netapip_freeTransInfo(pTransInfo);
1537                     *err = NETAPI_ERR_PA_FW;
1538                     netapip_netcpCfgDeleteClass(&netapi_get_global()->nwal_context, class_slot);
1539                     if(p_class->classType== NETCP_CFG_CLASS_TYPE_L3_L4)
1540                         n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1541                     return -1;
1542                 }
1543             }
1544             netapip_netcpCfgInsertClass(&netapi_get_global()->nwal_context, 
1545                                    class_slot,
1546                                  p_class->classType, 
1547                                 NULL,  //L2 we have
1548                                 (p_class->classType== NETCP_CFG_CLASS_TYPE_L3_L4? l3_handle : NULL),
1549                                   pTransInfo->handle,
1550                                   user_data);
1552             netapip_freeTransInfo(pTransInfo);
1553             if(p_class->classType== NETCP_CFG_CLASS_TYPE_L3_L4)
1554                 n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1555             return classHandle;
1556     } //end switch
1558 return -1;
1561 /********************************************************************
1562  * FUNCTION PURPOSE:  API to delete a preconfigured classifier
1563  ********************************************************************
1564  * DESCRIPTION:  API to delete a preconfigured classifier
1565  ********************************************************************/
1566 void netapi_netcpCfgDelClass(NETAPI_T           h,
1567                              NETCP_CFG_CLASS_T  classId,
1568                              int*               err)
1570     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1571     void *L4_handle = NULL;  //class handle -> L4
1572     void *L3_handle = NULL;  //class handle -> L3
1573     nwal_RetValue       retValue;
1574     NetapiNwalTransInfo_t *pTransInfo;
1575     nwal_TransID_t     trans_id;
1576     int class_slot=-1;
1578     class_slot = netapi_cfgGetMatchId(classId);
1579     L4_handle=netapip_netcpCfgGetL4Handle(&netapi_get_global()->nwal_context, class_slot);
1580     if(!L4_handle)
1581     {
1582         *err = NETAPI_ERR_BAD_INPUT; 
1583         goto ERR_netapi_netcpCfgDelClass;
1584     }
1585     L3_handle = netapip_netcpCfgGetL3Handle( &netapi_get_global()->nwal_context,
1586                                             class_slot);
1587     /* l3 handle might be NULL, depending on type of classifier */
1589     if(L3_handle)
1590     {
1591         // grab l3 lock
1592         n->spinLock.lock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1593     }
1594     netapip_netcpCfgDeleteClass(
1595                         &netapi_get_global()->nwal_context,
1596                         class_slot );
1597     //get a transaction id
1598     pTransInfo = netapip_getFreeTransInfo(n,
1599                                           (NETAPI_PROC_GLOBAL_T *) n->proc_global,
1600                                           &trans_id);
1601     if (!pTransInfo)
1602     {
1603         *err =  NETAPI_ERR_BUSY; 
1604         goto ERR_netapi_netcpCfgDelClass;
1605     }
1606     pTransInfo->transType = NETAPI_NWAL_HANDLE_TRANS_PORT;
1607     pTransInfo->state = NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING;
1608     pTransInfo->inUse = nwal_TRUE;
1609     pTransInfo->netapi_handle = h;
1610     //issue request for L4
1611     retValue = nwal_delConn(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,
1612                                                  trans_id,
1613                                                  L4_handle);
1614     if(retValue !=  nwal_OK)
1615     {
1616         *err = NETAPI_ERR_NWAL_ERR0;
1617          netapip_freeTransInfo(pTransInfo);
1618          goto ERR_netapi_netcpCfgDelClass;  /* todo: what about the L3? */
1619     }
1620     //wait here until its done since scheduler isn't running yet most likely..
1621     // todo:  make this handled by scheduler poll later ??
1622     if(trans_id != NWAL_TRANSID_SPIN_WAIT)
1623     {
1624         n->nwal_local.numPendingCfg++;
1625         while ((pTransInfo->state  !=NETAPI_NWAL_HANDLE_STATE_ERR) &&
1626                 (pTransInfo->state !=NETAPI_NWAL_HANDLE_STATE_IDLE))
1627         {
1628             // if response is there, then this poll squirts out in the CTl poll callback, 
1629             // which handles the rest (including decrmenting #pending!!
1630             nwal_pollCtl(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle,NULL,NULL);
1631         }
1632     }
1633     if (pTransInfo->state == NETAPI_NWAL_HANDLE_STATE_ERR)
1634     {
1635         netapip_freeTransInfo(pTransInfo);
1636         *err = NETAPI_ERR_PA_FW;
1637          goto ERR_netapi_netcpCfgDelClass;
1638     }
1639     pTransInfo->state =  NETAPI_NWAL_HANDLE_STATE_IDLE;
1640     pTransInfo->inUse = nwal_FALSE;
1642     /* delete L3 if we have to */
1643     if (cfgOkToDeleteL3ClassiHandle(h, L3_handle))
1644     {
1645         netapip_netcpCfgDelIpInternal(h,
1646                                       0,
1647                                       0,
1648                                       NULL,
1649                                       NULL,
1650                                       0,
1651                                       err,
1652                                       L3_handle,
1653                                       0);
1654     }
1655     netapip_freeTransInfo(pTransInfo);
1657 ERR_netapi_netcpCfgDelClass:
1658     if(L3_handle)
1659         n->spinLock.unlock(&pnetapiShm->netapi_netcp_cfg_l3_classi_lock);
1661     return;
1664 /********************************************************************
1665  * FUNCTION PURPOSE:  API to add a flow
1666  ********************************************************************
1667  * DESCRIPTION:  API to add a flow
1668  ********************************************************************/
1669 /*--------------flow management--------*/
1670 // ADD A Flow
1671 NETCP_CFG_FLOW_HANDLE_T netapi_netcpCfgAddFlow(NETAPI_T                 h,
1672                                                int                      n,
1673                                                Pktlib_HeapHandle        handles[],
1674                                                int                      sizes[],
1675                                                NETCP_CFG_FLOW_CONFIG_T* p_cfg,
1676                                                int*                     err )
1678     Cppi_RxFlowCfg  rxFlowCfg;
1679     Uint8           isAlloc;
1680     Qmss_QueueHnd   rxBufQ[TUNE_NETAPI_MAX_BUF_POOLS_IN_FLOW];
1681     Uint32          rxBufSize[TUNE_NETAPI_MAX_BUF_POOLS_IN_FLOW];
1682     int             i;
1683     Cppi_FlowHnd FlowHnd;
1684     int  slot;
1685     NETCP_CFG_FLOW_HANDLE_T retVal;
1686     NETAPI_HANDLE_T * pHandle = (NETAPI_HANDLE_T *) h;
1687     *err= 0;  /* ok */
1688     if (!p_cfg)
1689     {
1690         *err= NETAPI_ERR_BAD_INPUT;
1691         return NULL;
1692     }
1693     //get a slot to save new flow
1694     slot = netapip_netcpCfgFindFlowSlot(pHandle,
1695                                         &netapi_get_global()->nwal_context);
1697     if (slot<0) { *err= NETAPI_ERR_NOMEM;  return NULL; }
1699     //configure flow
1700     memset(&rxFlowCfg,0,sizeof(Cppi_RxFlowCfg));
1701     for (i = 0; i < TUNE_NETAPI_MAX_BUF_POOLS_IN_FLOW; i++)
1702     {
1703         if (i >= n)
1704         {
1705             rxBufQ[i] = 0;
1706             rxBufSize[i] = 0;
1707         }
1708         else
1709         {
1710             rxBufQ[i] =   Pktlib_getInternalHeapQueue(handles[i]);
1711             //todo: verity sizes< heapsize
1712             //todo: verify order
1713             rxBufSize[i]= sizes[i];
1714         }
1715        if (i && (rxBufQ[i] <= 0))
1716        {
1717             rxBufQ[i] = rxBufQ[i-1];
1718             rxBufSize[i] = 0;
1719        }
1720     }
1722     /* Configure Rx flow */
1723     rxFlowCfg.flowIdNum      = p_cfg->flow_index;/*CPPI_PARAM_NOT_SPECIFIED*/;
1724     rxFlowCfg.rx_dest_qnum   = netapi_pktioGetQ(p_cfg->p_dest_q);
1725     rxFlowCfg.rx_dest_qmgr   = Qmss_getQueueNumber(rxFlowCfg.rx_dest_qnum).qMgr; 
1726     rxFlowCfg.rx_sop_offset  = p_cfg->recv_offset;
1727     rxFlowCfg.rx_ps_location = Cppi_PSLoc_PS_IN_DESC;
1728     rxFlowCfg.rx_desc_type   = Cppi_DescType_HOST;
1729     rxFlowCfg.rx_error_handling = p_cfg->block;
1731     rxFlowCfg.rx_psinfo_present = 1;
1732     rxFlowCfg.rx_einfo_present  = 1;
1734     rxFlowCfg.rx_dest_tag_lo = 0;
1735     rxFlowCfg.rx_dest_tag_hi = 0;
1736     rxFlowCfg.rx_src_tag_lo  = 0;
1737     rxFlowCfg.rx_src_tag_hi  = 0;
1739     rxFlowCfg.rx_size_thresh0_en = rxBufSize[1] ? 1 : 0;
1740     rxFlowCfg.rx_size_thresh1_en = rxBufSize[2] ? 1 : 0;
1741     rxFlowCfg.rx_size_thresh2_en = rxBufSize[3] ? 1 : 0;
1743   if (p_cfg->dma_index==NETAPI_DMA_NETCP )
1744     rxFlowCfg.rx_dest_tag_lo_sel =0;
1745   else
1746     rxFlowCfg.rx_dest_tag_lo_sel = 0x4;
1747     rxFlowCfg.rx_dest_tag_hi_sel = 0;
1748     rxFlowCfg.rx_src_tag_lo_sel  = 0;
1749     rxFlowCfg.rx_src_tag_hi_sel  = 0;
1751     rxFlowCfg.rx_fdq1_qnum = rxBufQ[1];
1752     rxFlowCfg.rx_fdq1_qmgr = (rxBufQ[1] ? Qmss_getQueueNumber(rxBufQ[1]).qMgr: 0);
1753     rxFlowCfg.rx_fdq2_qnum = rxBufQ[2];
1754     rxFlowCfg.rx_fdq2_qmgr = (rxBufQ[2] ? Qmss_getQueueNumber(rxBufQ[2]).qMgr: 0);
1755     rxFlowCfg.rx_fdq3_qnum = rxBufQ[3];
1756     rxFlowCfg.rx_fdq3_qmgr = (rxBufQ[3] ? Qmss_getQueueNumber(rxBufQ[3]).qMgr: 0);
1758     rxFlowCfg.rx_size_thresh0 = rxBufSize[1] ? rxBufSize[0] : 0;
1759     rxFlowCfg.rx_size_thresh1 = rxBufSize[2] ? rxBufSize[1] : 0;
1760     rxFlowCfg.rx_size_thresh2 = rxBufSize[3] ? rxBufSize[2] : 0;
1762     rxFlowCfg.rx_fdq0_sz0_qnum = rxBufQ[0];
1763     rxFlowCfg.rx_fdq0_sz0_qmgr =  (rxBufQ[0] ? Qmss_getQueueNumber(rxBufQ[0]).qMgr: 0);
1764     rxFlowCfg.rx_fdq0_sz1_qnum = rxBufQ[1];
1765     rxFlowCfg.rx_fdq0_sz1_qmgr =  (rxBufQ[1] ? Qmss_getQueueNumber(rxBufQ[1]).qMgr:0);
1766     rxFlowCfg.rx_fdq0_sz2_qnum = rxBufQ[2];
1767     rxFlowCfg.rx_fdq0_sz2_qmgr =  (rxBufQ[2] ? Qmss_getQueueNumber(rxBufQ[2]).qMgr:0);
1768     rxFlowCfg.rx_fdq0_sz3_qnum = rxBufQ[3];
1769     rxFlowCfg.rx_fdq0_sz3_qmgr =  (rxBufQ[3] ? Qmss_getQueueNumber(rxBufQ[3]).qMgr:0);
1770     {
1771         //todo: replace this with a nwal call to get global cntx info
1772         Cppi_CpDmaInitCfg cpdmaCfg;
1773         Cppi_Handle cppiHnd;
1774         memset(&cpdmaCfg,0,sizeof(Cppi_CpDmaInitCfg));
1775         cpdmaCfg.dmaNum   = (p_cfg->dma_index==NETAPI_DMA_NETCP ) ?Cppi_CpDma_PASS_CPDMA: Cppi_CpDma_QMSS_CPDMA;
1776         cppiHnd= Cppi_open (&cpdmaCfg);
1777         if (cppiHnd == NULL)
1778         {
1779               netapip_netcpCfgDeleteFlow(&netapi_get_global()->nwal_context, slot);
1780               *err= NETAPI_ERR_NORES;
1781               return NULL;
1782         }
1783         FlowHnd = Cppi_configureRxFlow (cppiHnd, &rxFlowCfg, &isAlloc);
1784     }
1785     if (FlowHnd == NULL)
1786     {
1787         *err= NETAPI_ERR_NORES;
1788         netapip_netcpCfgDeleteFlow(&netapi_get_global()->nwal_context, slot);
1789         return (NULL);
1790     }
1792     //update slot
1793     retVal = netapip_netcpCfgInsertFlow(&netapi_get_global()->nwal_context,
1794                                         slot,
1795                                         p_cfg->dma_index,
1796                                         (void*) FlowHnd);
1797     return (retVal);
1800 /********************************************************************
1801  * FUNCTION PURPOSE:  API to delete a flow
1802  ********************************************************************
1803  * DESCRIPTION:  API to delete a flow
1804  ********************************************************************/
1805 void netapi_netcpCfgDelFlow(NETAPI_T                h,
1806                             NETCP_CFG_FLOW_HANDLE_T f,
1807                             int*                    err)
1809     int slot;
1810     void * handle;
1811     *err=0;
1812     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1813     /* find entry */
1814         slot = netapip_netcpCfgFindFlow(n,
1815                                         &netapi_get_global()->nwal_context,
1816                                         ((NETCP_CFG_FLOW_T *) f) ->flowid,
1817                                         ((NETCP_CFG_FLOW_T *) f) ->dma_engine,
1818                                         &handle);
1819     if (slot<0)
1820     {
1821         *err = NETAPI_ERR_BAD_INPUT;
1822         return;
1823     }
1824     if ((Cppi_closeRxFlow( (Cppi_FlowHnd) handle)) == 0)
1825     {
1826         netapip_netcpCfgDeleteFlow(&netapi_get_global()->nwal_context,
1827                                slot);
1828     }
1829     return;
1832 /******************************************************************************
1833  * FUNCTION PURPOSE:  API to configure NETCP with global rules for exception packet handling
1834  ******************************************************************************
1835  * DESCRIPTION:  API to configure NETCP with global rules for exception packet handling
1836  *****************************************************************************/
1837 NETCP_CFG_EXCEPTION_PKT_T netapi_netcpCfgExceptions(NETAPI_T                    h,
1838                                                     int                         exception_id,
1839                                                     nwal_matchAction_t          action,
1840                                                     NETCP_CFG_ROUTE_HANDLE_T    p_route)
1842     nwalCtlInfo_t   ctrl;
1843     NETAPI_HANDLE_T * n = (NETAPI_HANDLE_T *) h;
1844     nwal_RetValue retVal = 0;
1845     memset(&ctrl, 0, sizeof(nwalCtlInfo_t));
1847     if (p_route != NULL)
1848     {
1849         if((p_route->valid_params & NETCP_CFG_VALID_PARAM_ROUTE_TYPE) ==
1850             NETCP_CFG_VALID_PARAM_ROUTE_TYPE)
1851         {
1852            ctrl.validParams |= 
1853                     NWAL_CONTROL_VALID_PARAM_ROUTE_TYPE;
1854            ctrl.routeType = p_route->routeType;
1855         }
1856         netapip_netcpCfgBuildRoute(p_route,
1857                                    &ctrl.appRxPktFlowId,
1858                                    &ctrl.appRxPktQueue,
1859                                    &ctrl.routeType);
1860     }
1861     else
1862     {
1863         ctrl.appRxPktFlowId = NWAL_FLOW_NOT_SPECIFIED;
1864         ctrl.appRxPktQueue = NWAL_QUEUE_NOT_SPECIFIED;
1865     }
1867     ctrl.appId = (void*)(NETAPI_NETCP_CFG_MATCH_EXCEPTION | exception_id);
1869     ctrl.matchAction = action;
1871     if (exception_id == NETCP_CFG_ALL_EXCEPTIONS)
1872     {
1873         ctrl.pktCtl = NWAL_CTRL_CFG_ALL_EXCEPTIONS;
1874     }
1875     else
1876     {
1877             ctrl.pa_EROUTE_Id = exception_id;
1878             ctrl.pktCtl = NWAL_CTRL_CFG_SINGLE_EXCEPTION;
1879     }
1881     retVal = nwal_control(((NETAPI_GLOBAL_T*) (n->global))->nwal_context.nwalInstHandle, &ctrl);
1884     return (NETCP_CFG_EXCEPTION_PKT_T) ctrl.appId;
1887 /*************************************************************************/
1888 /*********************************INTERNAL*******************************/
1889 /************************************************************************/
1891 /********************************************************************
1892  * FUNCTION PURPOSE:  NETAPI internal function, NETCP command reply callback
1893  ********************************************************************
1894  * DESCRIPTION:  NETAPI internal function, NETCP command reply callback
1895  ********************************************************************/
1896 void netapip_netcpCfgNWALCmdCallBack(nwal_AppId        appHandle,
1897                             uint16_t            trans_id,
1898                             nwal_RetValue     ret)
1900     NetapiNwalTransInfo_t * p_trans;
1901     NETAPI_NWAL_LOCAL_CONTEXT_T *p_local=NULL;
1903     if(trans_id == NWAL_TRANSID_SPIN_WAIT)
1904     {
1905         netapi_get_proc_global()->nwal_context.numBogusTransIds++;
1906         return;
1907     }
1909     p_trans= &netapi_get_proc_global()->nwal_context.transInfos[trans_id];
1910     p_local =&((NETAPI_HANDLE_T*) (p_trans->netapi_handle))->nwal_local;
1912     if(ret != nwal_OK)
1913     {
1914         // update error code that is fialed  in p_trans */
1915         //todo: atomic inc
1916         p_trans->state = NETAPI_NWAL_HANDLE_STATE_ERR;
1917         netapi_get_proc_global()->nwal_context.numCmdFail++;
1918     }
1919     else
1920     {
1921         //todo: atomic inc
1922         netapi_get_proc_global()->nwal_context.numCmdPass++;
1923         switch(p_trans->transType)
1924         {
1925             case NETAPI_NWAL_HANDLE_TRANS_MAC:
1926             {
1927                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1928                 {
1929                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1930                 }
1931                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1932                 {
1933                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1934                 }
1935                 break;
1936             }
1937             case NETAPI_NWAL_HANDLE_TRANS_IP:
1938             {
1939                  if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1940                 {
1941                     p_trans->state = NETAPI_NWAL_HANDLE_STATE_OPEN;
1942                 }
1943                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1944                 {
1945                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1946                 }
1947                 break;
1948             }
1949             case NETAPI_NWAL_HANDLE_TRANS_PORT:
1950             {
1951                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1952                 {
1953                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1954                 }
1955                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1956                 {
1957                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1958                 }
1959                 break;
1960             }
1961             case NETAPI_NWAL_HANDLE_TRANS_SA:
1962             {
1963                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1964                 {
1965                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1966                 }
1967                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1968                 {
1969                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1970                 }
1971                 break;
1972             }
1973             case NETAPI_NWAL_HANDLE_TRANS_SA_POLICY:
1974             {
1975                 if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_OPEN_PENDING)
1976                 {
1977                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_OPEN;
1978                 }
1979                 else if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_CLOSE_PENDING)
1980                 {
1981                     p_trans->state =NETAPI_NWAL_HANDLE_STATE_IDLE;
1982                 }
1983                 break;
1984             }
1985             default:
1986             {
1987                 break;
1988             }
1989         }
1990     }
1992     p_local->numPendingCfg--;
1994     if(p_trans->state == NETAPI_NWAL_HANDLE_STATE_IDLE)
1995     {
1996         p_trans->inUse = nwal_FALSE;
1997     }
2001 /********************************************************************
2002  * FUNCTION PURPOSE:  NETAPI internal function, PA stats reply callback
2003  ********************************************************************
2004  * DESCRIPTION:  NETAPI internal function, PA stats reply callback
2005  ********************************************************************/
2006 void netapip_netcpCfgNWALCmdPaStatsReply(nwal_AppId        appHandle,
2007                                 nwal_TransID_t    trans_id,
2008                                 paSysStats_t      *stats)
2010     NetapiNwalTransInfo_t * p_trans;
2011     NETAPI_NWAL_LOCAL_CONTEXT_T *p_local=NULL;
2013     if(trans_id == NWAL_TRANSID_SPIN_WAIT)
2014     {
2015         netapi_get_proc_global()->nwal_context.numBogusTransIds++;
2016         return;
2017     }
2019     p_trans= &netapi_get_proc_global()->nwal_context.transInfos[trans_id];
2020     p_trans->inUse = nwal_FALSE;
2021     p_local =&((NETAPI_HANDLE_T*) (p_trans->netapi_handle))->nwal_local;
2023     //save a local copy  of some stuff*/
2024     p_local->numL2PktsRecvd=stats->classify1.nPackets;
2025     p_local->numL3PktsRecvd=stats->classify1.nIpv4Packets;
2027     //callout result to application !!
2028     if (p_local->stats_cb)
2029     {
2030         (*p_local->stats_cb)(p_trans->netapi_handle,stats);
2031     }